Format Specifiers in C
When you print a variable with printf(), you must tell C what type of value it is. You do this with a small code starting with %, called a format specifier.
%d for integers, %f for decimals, %c for characters... In this lesson you will learn the complete list with examples.
What You Will Learn
- What a format specifier is.
- The complete list of format specifiers.
- Printing the same value in different formats.
- Printing a
%sign. - Which specifiers are different in
printf()andscanf().
Format Specifiers in C Explained
Exam definition: A format specifier is a sequence beginning with % that tells printf() or scanf() the type of data to print or read.
Complete list of format specifiers
| Specifier | Used for | Example output |
|---|---|---|
%d or %i | int (signed decimal) | 25 |
%u | unsigned int | 4000000000 |
%ld | long | 123456789 |
%lld | long long | 9000000000 |
%hd | short | 2026 |
%f | float and double in printf | 3.140000 |
%lf | double (needed in scanf) | 3.140000 |
%Lf | long double | 3.140000 |
%e | Scientific notation | 3.140000e+00 |
%c | char | A |
%s | String | Hello |
%x / %X | Hexadecimal (small / capital letters) | 1a / 1A |
%o | Octal | 32 |
%p | Memory address (pointer) | 0x7ffe5c1a2b3c |
%zu | Result of sizeof | 4 |
%% | Prints a % sign | % |
Memory trick: The letter tells the type: d = decimal integer, f = float, c = character, s = string, x = hex, o = octal, p = pointer, u = unsigned. Add l for long.
%f vs %lf
- In
printf(), bothfloatanddoubleprint with%f(%lfalso works). - In
scanf(), you must use%fforfloatand%lffordouble. %fprints 6 digits after the decimal point by default. Use%.2ffor 2 digits.
Syntax: Using a Format Specifier
printf("text %specifier text", value);
printf("Age: %d, Grade: %c\n", age, grade); /* one specifier per value, in order */
The values are matched to the specifiers from left to right. The first specifier gets the first value, and so on.
Example 1: One Value, Many Formats
#include <stdio.h>
int main(void)
{
int n = 65;
float pi = 3.14159f;
printf("As integer : %d\n", n);
printf("As character : %c\n", n);
printf("As hexadecimal: %x\n", n);
printf("As octal : %o\n", n);
printf("Float default : %f\n", pi);
printf("Float 2 digits: %.2f\n", pi);
printf("Scientific : %e\n", pi);
printf("Discount : 50%%\n");
return 0;
}
Output
As integer : 65 As character : A As hexadecimal: 41 As octal : 101 Float default : 3.141590 Float 2 digits: 3.14 Scientific : 3.141590e+00 Discount : 50%
Code Explanation
%dprints 65 as a normal number.%cprints the character whose ASCII code is 65, which is A.%xprints 65 in hexadecimal: 4×16 + 1 = 65, so 41.%oprints 65 in octal: 1×64 + 0×8 + 1 = 65, so 101.%fprints 6 digits after the point;%.2frounds to 2 digits.%eprints 3.14159 as 3.141590 × 10⁰.%%prints one%sign.
How Format Specifiers Work
printf()reads the text from left to right and copies it to the screen.- When it finds a
%, it takes the next value from the list and converts it according to the letter after%. - The value in memory does not change. Only how it is displayed changes, which is why 65 can be shown as 65, A, 41 or 101.
- If the specifier and the value type don't match, the output is wrong (and it is undefined behaviour).
Example 2: Printing a Student Report
#include <stdio.h>
int main(void)
{
char name[] = "Riya";
int roll = 12;
char grade = 'A';
float percent = 91.456f;
printf("Name: %s\n", name);
printf("Roll: %d | Grade: %c\n", roll, grade);
printf("Percentage: %.1f%%\n", percent);
return 0;
}
Output
Name: Riya Roll: 12 | Grade: A Percentage: 91.5%
%s prints the whole string. %.1f rounds 91.456 to one decimal place: 91.5.
Important Points to Remember
- Every format specifier starts with
%. %dint,%ffloat,%cchar,%sstring.%lfis required for double in scanf().%%prints a percent sign.- Values match specifiers in order, left to right.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
printf("%d", 3.5); | Garbage output | Use %f for decimals |
printf("%f", 10); | Prints 0.000000 or garbage | Use %d, or write 10.0 |
printf("50%"); | Unpredictable output | Use 50%% |
| Fewer values than specifiers | Garbage values printed | One value for every specifier |
scanf("%f", &d); with double d | Wrong value stored | Use %lf for double in scanf |
Format Specifiers Practice Questions
- What is a format specifier?
- Write the format specifiers for int, float, char, string and double (in scanf).
- How do you print the
%symbol with printf? - What does
printf("%c", 66);print? - What does
printf("%.3f", 2.5);print?
Show answers
- A code starting with
%that tells printf/scanf the type of data to print or read. %d,%f,%c,%s,%lf.- With
%%. B(ASCII 66).2.500
Coding Exercise
Store the number 255 in an int. Print it in decimal, hexadecimal (capital letters) and octal on separate lines.
Show solution
#include <stdio.h>
int main(void)
{
int n = 255;
printf("Decimal: %d\n", n);
printf("Hex : %X\n", n);
printf("Octal : %o\n", n);
return 0;
}
Decimal: 255 Hex : FF Octal : 377
Quick Quiz: Format Specifiers in C (MCQ)
-
Which format specifier prints a string?
A)
%cB)%sC)%dD)%f -
Which is required to read a
doublewith scanf?A)
%fB)%dC)%lfD)%ld -
What does
printf("%x", 255);print?A) 255 B) ff C) 377 D) FF
-
Which prints a memory address?
A)
%aB)%mC)%pD)%x -
By default,
%fprints how many digits after the decimal point?A) 2 B) 4 C) 6 D) 8
Show answers
- B:
%s - C:
%lf - B:
ff(small letters with%x). - C:
%p - C: 6.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| int | %d or %i |
|---|---|
| float | %f |
| double | %f in printf, %lf in scanf |
| char / string | %c / %s |
| unsigned / long long | %u / %lld |
| hex / octal | %x / %o |
| address | %p |
| percent sign | %% |
Frequently asked questions
- What is the difference between %d and %i?
- In printf they are the same. In scanf,
%ialso reads octal and hex input (like 012 or 0x1A). - Why use %lf for double in scanf?
- scanf needs to know the exact size to store:
%f= 4-byte float,%lf= 8-byte double. - How do you print % in printf?
- Write
%%. - What happens if the format specifier is wrong?
- The output is wrong or garbage; it is undefined behaviour.
Remember
- Letter = type: decimal, float, char, string, hex, octal, pointer.
%.2f= 2 digits after the point.
Next Topic
Next lesson: Escape Sequences in C. You will learn special characters like \n and \t that control how text is printed.