In C programming there are number of data types and format specifiers is used to defines the type of data to be printed.
Whether to print output or to take input in both case we required format specifiers. Format specifiers are also called as format string.
list of format specifiers used in C language.
Format specifier | Description | Supported data types |
---|---|---|
%c | Character | char unsigned char |
%d | Signed Integer | short unsigned short int long |
%e or %E | Scientific notation of float values | float double |
%f | Floating point | float |
%g or %G | Similar as %e or %E | float double |
%hi | Signed Integer(Short) | short |
%hu | Unsigned Integer(Short) | unsigned short |
%i | Signed Integer | short unsigned short int long |
%l or %ld or %li | Signed Integer | long |
%lf | Floating point | double |
%Lf | Floating point | long double |
%lu | Unsigned integer | unsigned int unsigned long |
%s | String | char * |
%u | Unsigned Integer | unsigned int unsigned long |
How to print integer character float and double using format string
Print integer : we can print integer value by using %d. for example:
1 2 | int i = 10; printf(“ value of i = %d” , i ); |
Result
1 | value of i = 10 |
Here whatever written within double code “ ” print as it is. %d is used to tell compiler print integer value. And value of “ i (integer value) ” will be print.
Print multiple integer : suppose we want to print two integer value then we need to write two times %d. for example:
1 2 3 | int i = 10; int j=20; printf(“ value of i & j = %d%d” , i,j ); |
Output
1 | value of i & j = 10 20 |
Here whatever written within double code “ ” print as it is. %d%d is used to tell compiler print two integer value. And value of “ i & j (integer value) ” will be print.
Print Character and float value : suppose we want to print two character and float value then we need two format string %c and %f respectively. for example:
1 2 3 | char c = ‘A’; float f=20.11; printf(“ value of c & f = %c%f” , c,f ); |
1 | value of c & j = A 20.1100 |
Here whatever written within double code “ ” print as it is. %d%d is used to tell compiler print character and float value. And value of “ c & f ” will be print.
How to take input from user : scanf()
scanf() function is used to read character, string, numeric data from keyboard. The scanf() function allows you to accept input from keyboard.
Take integer input from user : we can take input integer value by user using %d format string. for example:
1 2 | int i ; scanf(“ %d” , &i ); |
above line will read an integer value that the user enters on the keyboard. %d is used to tell compiler to read integers value from keyboard and store in a integer variable i.
Take multiple integer input from user: we can take two integer value as s input by user using two times %d format string. for example:
1 2 | int i ,j; scanf(“ %d%d” ,& i, &j ); |
above line will read two integer value that the user enters on the keyboard. Two times %d is used to tell compiler to read two integers value from keyboard and store in a integer variable i & j
Note: similarly to take char, float value from user %c & %f formatted string used in a scanf() function.