Format Specifiers in C
A format specifier is a special symbol used with printf() and
scanf(). It tells the compiler what type of value we want to print or take as input.
Format specifiers always start with the % symbol.
This page covers the most commonly used format specifiers. These are enough for most beginner programs, exam answers and daily practice.
Most Used Format Specifiers in C
| Format Specifier | Used For | Data Type | Example |
|---|---|---|---|
| %d | Integer value | int |
int age = 20; |
| %f | Decimal value | float |
float marks = 88.5; |
| %.2f | Decimal value with fixed digits after point | float or double |
printf("%.2f", marks); |
| %lf | Double input in scanf() |
double |
scanf("%lf", &price); |
| %c | Single character | char |
char grade = 'A'; |
%s |
String or character array | char[] |
char name[] = "Ravi"; |
Need More Format Specifiers?
Some format specifiers are useful but not used in every beginner program, such
as
%u, %ld, %lld, %x, %o,
%p and %zu.
Study them separately here: Less Used Format Specifiers in C.
1. %d - Integer Format Specifier
%d is used to print or read integer values.
#include <stdio.h>
int main() {
int age = 20;
printf("Age = %d", age);
return 0;
}
Output:
Age = 20
Explanation:
ageis an integer variable.%dtellsprintf()to print an integer value.
2. %f - Float Format Specifier
%f is used to print decimal values. By default, it prints 6 digits
after decimal.
#include <stdio.h>
int main() {
float marks = 88.5;
printf("Marks = %f", marks);
return 0;
}
Output:
Marks = 88.500000
Explanation:
marksis a float variable.%fprints decimal values.- It prints 6 decimal places by default.
3. %.2f - Decimal Precision
%.2f prints only 2 digits after decimal and rounds the value.
#include <stdio.h>
int main() {
float percentage = 87.4567;
printf("Percentage = %.2f", percentage);
return 0;
}
Output:
Percentage = 87.46
Explanation:
%.2fprints 2 digits after decimal.- The value is rounded from
87.4567to87.46. - This is useful for marks, percentage, money and average values.
4. %lf - Double Input Format Specifier
%lf is mainly used with scanf() for taking
double input. For printing double with printf(), we normally use
%f.
#include <stdio.h>
int main() {
double price = 99.75;
printf("Price = %.2f", price);
return 0;
}
Output:
Price = 99.75
Explanation:
priceis a double variable.- In
printf(),%fprints double values. - In
scanf(), use%lfto take double input.
double price;
scanf("%lf", &price); // Correct for double input
5. %c - Character Format Specifier
%c is used to print or read a single character.
#include <stdio.h>
int main() {
char grade = 'A';
printf("Grade = %c", grade);
return 0;
}
Output:
Grade = A
Explanation:
gradestores one character.%cprints that character.- Character constants are written inside single quotes.
6. %s - String Format Specifier
%s is used to print or read a string. In C, a string is stored as a
character array.
#include <stdio.h>
int main() {
char name[] = "Devanshi";
printf("Name = %s", name);
return 0;
}
Output:
Name = Devanshi
Explanation:
nameis a character array.%sprints the complete string until null character\0.
Combined Example of Common Format Specifiers
#include <stdio.h>
int main() {
int rollNo = 101;
float percentage = 88.75;
char grade = 'A';
char name[] = "Devanshii";
printf("Name: %s\n", name);
printf("Roll No: %d\n", rollNo);
printf("Percentage: %.2f\n", percentage);
printf("Grade: %c", grade);
return 0;
}
Output:
Name: Devanshi
Roll No: 101
Percentage: 88.75
Grade: A
Explanation:
%sprints stringname.%dprints integerrollNo.%.2fprints float value up to 2 decimal places.%cprints charactergrade.
Very Important: %f and %lf Difference
- In
printf(), use%fto print bothfloatanddouble. - In
scanf(), use%fforfloatinput. - In
scanf(), use%lffordoubleinput. %.2fmeans print only 2 digits after decimal.
Summary:
- Format specifiers start with
%. %dis used for integer values.%fis used for decimal values in output.%.2fcontrols digits after decimal.%cis used for character and%sis used for string.- In
scanf(), use%lffor double input.