|
Format specifier |
Data type |
Example value |
Usage |
|
%d |
int |
25 |
Integer numbers |
|
%i |
int |
25 |
Integer input/output |
|
%f |
float |
12.5 |
Floating-point numbers |
|
%lf |
double |
12.567 |
Double input using scanf() |
|
%c |
char |
'A' |
Single character |
|
%s |
char array |
"Hello" |
String |
|
%u |
unsigned int |
25 |
Non-negative integer |
|
%ld |
long int |
123456L |
Long integer |
|
%lld |
long long int |
123456789LL |
Very large integer |
|
%x |
Unsigned integer |
255 |
Hexadecimal in
lowercase |
|
%X |
Unsigned integer |
255 |
Hexadecimal in
uppercase |
|
%p |
Pointer |
Address |
Memory address |
- %s displays the student's name.
- %d displays an integer.
- %f displays a floating-point value.
- %c displays a single character.
- %.2f displays a floating-point number with exactly two digits after the decimal point.
- %% displays the % symbol.
- \n moves the output to the next line.
|
Format |
Example statement |
Output |
|
%d |
printf("%d",
25); |
25 |
|
%5d |
printf("%5d",
25); |
25 |
|
%05d |
printf("%05d",
25); |
00025 |
|
%f |
printf("%f",
3.14159); |
3.141590 |
|
%.2f |
printf("%.2f",
3.14159); |
3.14 |
|
%.3f |
printf("%.3f",
3.14159); |
3.142 |
|
%-5d |
printf("%-5d",
25); |
25 |
- Width: Minimum character spaces for displaying a value.
- Precision: Number of digits after the decimal point.
- Minus (-): Left-aligns the value.
- Zero (0): Pads the value with leading zeros.
No comments:
Post a Comment