Nearby lessons

20 of 124

C - 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.

In simple words: a format specifier is a fill-in-the-blanks marker. printf sees %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 %
New here? The types in the examples below — 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:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int age = 20;
6 float marks = 87.5;
7 char grade = 'A';
8 char name[] = "Rahul";
9 
10 printf("Name : %s\n", name);
11 printf("Age : %d\n", age);
12 printf("Marks : %.2f\n", marks);
13 printf("Grade : %c\n", grade);
14 
15 return 0;
16}
Output
Name  : Rahul
Age   : 20
Marks : 87.50
Grade : A

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.

Common Mistake: 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.
Common Mistake: too few values. printf("%d %d\n", a); has two specifiers but only one value — the second %d prints whatever happens to be in memory.
In simple words: the specifier is a promise about the type. Break the promise and C does not stop you — it just prints something meaningless. Compile with 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
Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 float price = 99.5;
6 
7 printf("Right : %.2f\n", price); // matches - prints 99.50
8 printf("Also : %f\n", price); // matches - prints 99.500000
9 printf("Pct : 50%%\n"); // %% prints one literal %
10 
11 return 0;
12}
Output
Right : 99.50
Also  : 99.500000
Pct   : 50%
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5