Format Specifiers in C
Format Specifiers in C
The format specifiers are used in C for input and output purposes. Using this concept
the compiler can understand that what type of data is in a variable during taking input
using the scanf() function and printing using printf() function. Here is a list of format
specifiers.
%c Character
%d Signed integer
%f Float values
%g or %G Similar as %e or %E
%i Unsigned integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
These are the basic format specifiers. We can add some other parts with the format
specifiers. These are like below −
A minus symbol (-) sign tells left alignment
A number after % specifies the minimum field width. If string is less than the
width, it will be filled with spaces
A period (.) is used to separate field width and precision
Example
Live Demo
#include <stdio.h>
main() {
char ch = 'B';
printf("%d\n", x);
printf("%i\n", y);
float f = 12.67;
int a = 67;
printf("%s\n", str);
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
We can use these format specifiers for the scanf() function also in the same manner.
So we can take the input from scanf() like above how we have printed.