Nearby lessons
20 of 124C - Format Specifiers
A format specifier is the % placeholder that tells C what kind of value to print or read — %d for an integer, %f for a float, %c for one character, %s for text. This lesson lists the specifiers you will actually use, shows them printing real values, and explains what goes wrong when the specifier does not match the variable.
Format Specifiers — %d, %f, %c, %s and More
A format specifier is a placeholder written inside quotes that tells C what type of value to print (with printf) or read (with scanf). It always starts with a % sign.
%d, goes to the matching value, and prints it exactly in that place — in the same order you give them.
The most important format specifiers:
| Specifier | Prints | Example value |
|---|---|---|
%d |
Integer (int) | 20 |
%f |
Float with 6 decimal places by default | 87.500000 |
%.2f |
Float with exactly 2 decimal places | 87.50 |
%c |
One character | A |
%s |
String (text) | Rahul |
%ld |
Long integer | 1234567890 |
%lf |
Double (in scanf) | 3.141593 |
%x |
Integer in hexadecimal | ff |
%% |
A literal percent sign | % |
int, float, char — are covered properly in the next chapter, Variables & Data Types. For now, just read int age as "a whole number called age".
Specifiers in Action
Each specifier prints its matching value in the same order it appears in the string. Notice how %.2f rounds the marks to two decimals while %f would have printed six:
When the Specifier Does Not Match
C trusts you. If you promise an integer with %d and hand it a float, the compiler will usually let it through with a warning — and then print nonsense at runtime.
printf("%d", 3.14); does not print 3. It prints a garbage number, because printf reads the bytes of a float as if they were an int. Always match the specifier to the type.
printf("%d %d\n", a); has two specifiers but only one value — the second %d prints whatever happens to be in memory.
gcc program.c -Wall and the compiler will warn you before you ever see the garbage.
The table below is worth memorising as pairs — the type on the left always travels with the specifier on the right:
| Variable type | Use in printf | Use in scanf |
|---|---|---|
int |
%d |
%d |
float |
%f or %.2f |
%f |
double |
%f or %lf |
%lf |
char |
%c |
%c |
char[] (string) |
%s |
%s |
- A format specifier is a placeholder that tells C what type of value to print or read.
- Format specifiers: %d int, %f float, %.2f two decimals, %c char, %s string.
- %ld is a long integer, %lf is a double (in scanf), %x prints in hexadecimal.
- printf uses specifiers to print values; scanf uses the same specifiers to read them.
- A mismatched specifier does not crash the compiler — it prints garbage. Match them carefully.
- Escape sequences: \n new line, \t tab, \\ backslash, %% percent.