Simple Tutorials for Smart Learning
Format specifiers in C tell the compiler what type of data to expect when printing or scanning values using printf() and scanf() functions.
printf()
scanf()
They ensure that the correct data type is formatted properly.
When you use printf() or scanf(), the format specifier helps match the variable type so that the value is displayed or read correctly.
A format specifier starts with a percent sign % followed by a character that represents the data type.
%
Used in:
printf() – to display values
scanf() – to receive input from users
%d
printf
scanf
10
%f
10.500000
%c
A
%s
Hello
%lf
12.345678
%u
4294967295
%x
%X
a
%o
12
%p
0x7ffee3ac4c90
%%
#include <stdio.h> int main() { int age = 25; float height = 5.9; char grade = 'A'; printf("Age: %d\n", age); printf("Height: %f\n", height); printf("Grade: %c\n", grade); return 0; }
Age: 25 Height: 5.900000 Grade: API: 3.14
#include <stdio.h> int main() { char name[] = "Alice"; double balance = 12345.67; printf("Name: %s\n", name); printf("Balance: %lf\n", balance); return 0; }
%10d
%.2f
%10.3f
printf("%10.2f", 123.456); // Output: " 123.46"
int num; float rate; char letter; scanf("%d %f %c", &num, &rate, &letter);
Always use & with scanf() variables (except for arrays/strings).
&
Match the specifier to the correct data type.
Use .2f or .1f for limiting decimal places in floats/doubles.
.2f
.1f
scanf("%s", name) doesn’t need & because arrays decay to pointers.
scanf("%s", name)
3.14
%5d
" 25"
%-5d
"25 "
%02d
"07"