Format specifiers define the type of data. The value of a variable is stored in memory. So we can not directly print values of a variable from memory. Therefore C provides format specifier for printing values of variables that are stored in memory. C provide different types of format specifier for data types. Every format specifier starts with % percent sign.
Following is the list of format specifiers that can be used with the printf( ) function.
List of format specifiers in C
Data type | Format Specifier | |
---|---|---|
Integer | short signed | %d or %I |
short unsigned | %u | |
long signed | %ld | |
long unsigned | %lu | |
unsigned hexadecimal | %x | |
unsigned octal | %o | |
Real | float | %f |
double | %lf | |
Character | signed character | %c |
unsigned character | %c | |
String | %s |
Let’s see an example to demonstrate the use of format specifier:
Example:
1 2 3 4 5 6 7 8 9 10 |
int main() { int a = 10; float b = 3.14; char ch = 'a'; printf("%d\n", a); //Integer format specifier printf("%f\n", b); //Float format specifier printf("%c", ch); //Character format specifier } |
Output:
1 2 3 |
10 3.140000 a |
The %f for printing real values, %d for printing integer values, %c for printing character values.
The format specifier does not print on the screen, it displays the value stored in variables.
In this way, you can also print a string using %s format specifier.
You can use this format specifier between expressions.
Also, these can use with scanf(). You will understand the purpose of each format specifier in the program in our next post.
If we use %f as format specifier in int it give 0or %d for float it give some no explain how