C format specifiers
The function scanf() and printf() make use of conversion specification to specify the type and size of data or datatype. Every conversion specification or format specification start with a percentage sign % –
The format specifier is at the time of input and output. It is the way to tell to the compiler what type of data is in a variable at the time input using scanf() and printing using printf() functions.
Some examples are %d, %f, %c etc.
There are some Format specifiers in C programming given below
format specifiers for printf()
printf(char *format, arg1, arg2, …)
| Format specifier |
Meaning |
| %c |
Print a single character |
|
%i, %d |
printf a decimal integer |
|
%u |
print an unsigned decimal integer |
|
%x |
print an unsigned hexadecimal integer using a,b,c,d,e,f |
|
%X |
printf an unsigned hexadecimal integers using A,B,C,D,E,F |
|
%o |
print an unsigned octal integer |
|
%f |
print a floating point number |
|
%e |
print the floating point number in the exponential format |
|
%E |
Same as the %e. but it is print E for exponent |
|
%g |
print the floating point number in float %f or %e format whichever is shorter. |
|
%% |
print the % sign. |
|
%s |
print the string |
|
%p |
print the pointer |
Format specifier for scanf()
| Format specifier |
Meaning |
| %c |
read a single character |
|
%d |
read signed decimal integer |
|
%u |
read an unsigned decimal integer |
|
%x, %X |
read an unsigned hexadecimal integer |
|
%o |
read an unsigned octal integer |
|
%f |
read a floating point number |
|
%e |
read the floating point number in the exponential format |
|
%E |
read Same as the %e. but it is read E for exponent |
|
%g, %G |
read the floating point number in float %f or %e or %E format whichever is shorter. |
|
%s |
read the string |
Let us take example to understand format specifier
|
main()
{
marks;
printf("Enter the marks"); /* print as String */
scanf("%d",&marks); /* read integer type value from user */
printf("Entered marks is= %d",marks);
0;
}
|
|
main()
{
x=6;
y=8;
printf("Value of x is:", x);
printf("\nValue of y is:",y);
0;
}
|
|
main()
{
a=5;
b= -5;
printf("Value of a is:", a);
printf("\nValue of b is:",b);
0;
}
|