BASIC4MCU | C언어 | C언어 | printf 출력 형식 %c %s %d...
페이지 정보
작성자 키트 작성일2017-09-12 13:20 조회2,310회 댓글0건본문
Control Character Explanation %c a single character %d a decimal integer %i an integer %e scientific notation, with a lowercase "e" %E scientific notation, with an uppercase "E" %f a floating-point number %g use %e or %f, whichever is shorter %G use %E or %f, whichever is shorter %o an octal number %x unsigned hexadecimal, with lowercase letters %X unsigned hexadecimal, with uppercase letters %u an unsigned integer %s a string %x a hexadecimal number %p a pointer %n the argument shall be a pointer to an integer into which is placed the number of characters written so far %% a percent sign A field-length specifier may appear before the final control character to indicate the width of the field:
- h, when inserted inside %d, causes the argument to be a short int.
- l, when inserted inside %d, causes the argument to be a long.
- l, when inserted inside %f, causes the argument to be a double.
- L, when inserted inside %d or %f, causes the argument to be a long long or long double respecively.
An integer placed between a % sign and the format command acts as a minimum field width specifier, and pads the output with spaces or zeros to make it long enough. If you want to pad with zeros, place a zero before the minimum field width specifier:
%012dYou can also include a precision modifier, in the form of a .N where N is some number, before the format command:
%012.4dThe precision modifier has different meanings depending on the format command being used:
- With %e, %E, and %f, the precision modifier lets you specify the number of decimal places desired. For example, %12.6f will display a floating number at least 12 digits wide, with six decimal places.
- With %g and %G, the precision modifier determines the maximum number of significant digits displayed.
- With %s, the precision modifier simply acts as a maximum field length, to complement the minimum field length that precedes the period.
All of printf()'s output is right-justified, unless you place a minus sign right after the % sign. For example,
%-12.4fwill display a floating point number with a minimum of 12 characters, 4 decimal places, and left justified. You may modify the %d, %i, %o, %u, and %x type specifiers with the letter l and the letter h to specify long and short data types (e.g. %hd means a short integer). The %e, %f, and %g type specifiers can have the letter l before them to indicate that a double follows. The %g, %f, and %e type specifiers can be preceded with the character '#' to ensure that the decimal point will be present, even if there are no decimal digits. The use of the '#' character with the %x type specifier indicates that the hexidecimal number should be printed with the '0x' prefix. The use of the '#' character with the %o type specifier indicates that the octal value should be displayed with a 0 prefix.
Inserting a plus sign '+' into the type specifier will force positive values to be preceded by a '+' sign. Putting a space character ' ' there will force positive values to be preceded by a single space character.
You can also include constant escape sequences in the output string.
The return value of printf() is the number of characters printed, or a negative number if an error occurred.
//
변환 문자열 의미 %o 8진 정수 형식으로 출력 %d 10진 정수 형식으로 출력 %ld long형 10진 정수 형식으로 출력 %x 16진 정수 형식으로 출력 %u 부호 없는 10진 정수 형식으로 출력 %f 소수점 형식으로 출력 %e %E 지수 형식으로 출력 %g %G %e와 %f 중 짧은 쪽, 소수점에 이어지는 0은 생략 %c 문자 형식으로 출력 %s 문자열 형식으로 출력
설명 서식에 맞추어 표준 출력장치로 출력합니다. 위의 서식 변환 문자열을 참고하십시오.
참고로 서실 문자열에 옵션을 추가하여 좀더 다양하게 출력할 수 있습니다. 아래의 예를 참고하여 주십시오.
변환 문자열 출력 의미 printf("%d", 123) 123 표준 출력장치로 출력 printf("%5d", 123) ___123 10진수를 5자리에 맞추어 출력, 123앞에 공백 2개 추가 printf("%-5d", 123) 123__ 10진수를 5자리에 맞추어 출력, 왼쪽 맞춤, 오른쪽에 공백 추가 printf("%f", 1.234567) 1.234567 16진 정수 형식으로 출력 printf("%4f", 1.234567) 1.2346 소쉄 이하 4자리 출력, 반올림 printf("%7.2f", 1.234567) ___1.23 소숫점 포함해서 전체 7자리, 소수점 이하 2자리. 공백 3개가 앞 부분에 추가됩니다. printf("%s", "forum.falinux.com") forum.falinux.com 지수 형식으로 출력 printf("%20s", "forum.falinux.com") __________forum.falinux.com 자릿수를 맞추기 위해 왼쪽에 공백을 넣어 문자열 출력 printf("%10s", "forum.falinux.com") forum.falinux.com 문자열이 더 길면 그대로 출력 printf("%.10s", "forum.falinux.com") forum.fali 문자열이 더 길면 잘라서 출력 printf("%-20s", "forum.falinux.com") forum.falinux.com________ 문자열을 왼쪽 맟춤으로 하여 오른쪽에 모자르는 자리를 공백으로 메꿈 printf("%12.10s", "forum.falinux.com") __forum.fali 전체 12자리로 10자리만 출력, 모자른 부분은 왼쪽에 공백 추가하여 출력
헤더 stdio.h 형태 int printf (const char * format, ... );
인수 char *format 서식 문자열 반환 int 출력된 문자 수를 반환하며 오류가 발생하면 음수를 반환
예제 #include <stdio.h>int main( void){ char *str = "http://forum.falinux.com"; int ret; ret = printf( "%d %x %f %e %g %sn", 123, 123, 123.123, 123.123, 123.123, str); printf( "ret=%dn", ret); return 0;}
//아래는 코드비젼 메뉴얼의 내용입니다.void printf(char flash *fmtstr [ , arg1, arg2, ...])outputs formatted text, using putchar, according to the format specifiers in the fmtstr string.The format specifier string fmtstr is constant and must be located in FLASH memory.The implementation of printf is a reduced version of the standard C function.This was necessary due to the specific needs of an embedded system and because the fullimplementation would require a large amount of memory space.The following format specifiers are available:%c outputs the next argument as an ASCII character%d outputs the next argument as a decimal integer%i outputs the next argument as a decimal integer%u outputs the next argument as an unsigned decimal integer%x outputs the next argument as an unsigned hexadecimal integer using lower case letters%X outputs the next argument as an unsigned hexadecimal integer using upper case letters%s outputs the next argument as a null terminated character string, located in SRAM%% outputs the % characterAll numeric values are right aligned and left padded with spaces.If a 0 character is inserted between the % and d, i, u, x or X then the number will be left padded with0’s.If a - character is inserted between the % and d, i, u, x or X then the number will be left aligned.A width specifier between 1 and 9 can be inserted between the % and d, i, u, x or X to specify theminimum width of the displayed number.아래 코드는 설명을 해달라고 올라온 질문글인데sprintf()문을 사용하는 방법과 조금 다른 방식으로 만드는 코드를 추가 해봤습니다.
1 |
void LCD_1d1(float number){ |
2 |
unsigned int i,j; |
3 |
j=(int)(number*10.0+0.5); |
4 |
i=j/10; LCD_data(i+'0'); LCD_data('.'); |
5 |
i=j%10; LCD_data(i+'0'); |
6 |
} |
7 |
void LCD_1d1(float number){ |
8 |
char str[10]; |
9 |
댓글 0
조회수 2,310등록된 댓글이 없습니다.