Data type | Format | Description | Format | Description | Format | Description |
---|---|---|---|---|---|---|
short | %hd | decimal | %ho | octal | %hx | hexadecimal |
int | %d | decimal | %o | octal | %x | hexadecimal |
long | %ld | decimal | %lo | octal | %lx | hexadecimal |
long long | %lld | decimal | %llo | octal | %llx | hexadecimal |
unsigned short | %hu | decimal | %ho | octal | %hx | hexadecimal |
unsigned | %u | decimal | %o | octal | %x | hexadecimal |
unsigned long | %lu | decimal | %lo | octal | %lx | hexadecimal |
unsigned long long | %llu | decimal | %llo | octal | %llx | hexadecimal |
float | %f | fixed-point | %e | scientific notation | %g | %e or %f |
double | %lf | fixed-point | %le | scientific notation | %lg | %le or %lf |
long double | %Lf | fixed-point | %Le | scientific notation | %Lg | %Le or %Lf |
char | %c | character | ||||
array of char | %s | string |
Code | Output (“¤” means a space) |
---|---|
printf("%5d", 12) | ¤¤¤12 |
printf("%2d\n", 8675309) | 8675309 |
printf("%-5d", 12) | 12¤¤¤ |
printf("%05d", 12) | 00012 |
printf("%f", 12.345) | 12.345000 |
printf("%.1f", 12.345) | 12.3 |
printf("%6.1f", 12.345) | ¤¤12.3 |
printf("%-6.1f", 12.345) | 12.3¤¤ |
printf("%5o", 20) | ¤¤¤24 |
printf("%05o", 20) | 00024 |
printf("%3c", 'X') | ¤¤X |
printf("%-3c", 'X') | X¤¤ |
printf("%s", "abc") | abc |
printf("%5s", "abc") | ¤¤abc |
printf("%-5s", "abc") | abc¤¤ |
printf("%.2s", "abc") | ab |
printf("%-5.2s", "abc") | ab¤¤¤ |