Width and Precision in printf()
Look at a bill or a marksheet: numbers are neatly lined up in columns, and money always has 2 decimal places. You can do the same with printf().
In this lesson you will learn field width (how many spaces a value takes) and precision (how many decimal places or characters are shown).
What You Will Learn
- What field width and precision mean.
- Right and left alignment:
%5dand%-5d. - Padding with zeros:
%05d. - Decimal places:
%.2fand%8.2f. - Cutting a string:
%.3s.
Width and Precision in printf() Explained
Exam definition: Field width is the minimum number of character spaces used to print a value. Precision is the number of digits after the decimal point for floats, or the maximum number of characters printed for strings. Format: %[flag][width][.precision]specifier.
| Format | Meaning | Value | Output |
|---|---|---|---|
%5d | Width 5, right-aligned | 42 | [ 42] |
%-5d | Width 5, left-aligned | 42 | [42 ] |
%05d | Width 5, padded with zeros | 42 | [00042] |
%.2f | 2 decimal places | 3.14159 | [3.14] |
%8.2f | Width 8, 2 decimal places | 3.14159 | [ 3.14] |
%.3s | At most 3 characters | "Hello" | [Hel] |
The square brackets are only there to show the spaces. They are not printed by the format.
Memory trick: Number before the dot = width (the size of the box). Number after the dot = precision (how much detail). Minus = move left. Zero = fill with zeros.
Rules
- Width is a minimum. If the value is longer, it is printed in full:
%2dwith 12345 prints12345. - Without a minus sign, values are right-aligned (spaces on the left).
- Precision on a float rounds the value:
%.1fwith 2.66 prints2.7.
Syntax of Width and Precision
%[flag][width][.precision]specifier
%8d /* width 8 */
%-8d /* width 8, left-aligned */
%08d /* width 8, zero-padded */
%.3f /* 3 decimal places */
%10.3f /* width 10, 3 decimal places */
%.4s /* first 4 characters of a string */
Example 1: See Every Format in Action
#include <stdio.h>
int main(void)
{
int n = 42;
float pi = 3.14159f;
printf("[%5d]\n", n);
printf("[%-5d]\n", n);
printf("[%05d]\n", n);
printf("[%.2f]\n", pi);
printf("[%8.2f]\n", pi);
printf("[%.3s]\n", "Hello");
return 0;
}
Output
[ 42] [42 ] [00042] [3.14] [ 3.14] [Hel]
Code Explanation
%5d: 42 has 2 digits, so 3 spaces are added on the left to make 5.%-5d: the minus moves 42 to the left; 3 spaces go on the right.%05d: the 0 flag fills the empty places with zeros instead of spaces.%.2f: rounds 3.14159 to 2 decimal places: 3.14.%8.2f: "3.14" is 4 characters; 4 spaces are added to make 8.%.3s: prints only the first 3 characters of "Hello".
How Width and Precision Work
- printf first converts the value to text using the precision (e.g. 3.14159 → "3.14").
- Then it compares the text length with the width.
- If the text is shorter, it adds spaces (or zeros) to reach the width, on the left by default or on the right with
-. - If the text is longer, it prints it as it is. Width never cuts a number.
Example 2: Print a Neat Bill Table
#include <stdio.h>
int main(void)
{
printf("%-10s %5s %10s\n", "Item", "Qty", "Amount");
printf("%-10s %5d %10.2f\n", "Pen", 4, 40.0);
printf("%-10s %5d %10.2f\n", "Notebook", 2, 130.5);
printf("%-10s %5d %10.2f\n", "Eraser", 10, 5.0);
return 0;
}
Output
Item Qty Amount Pen 4 40.00 Notebook 2 130.50 Eraser 10 5.00
Text is left-aligned with %-10s; numbers are right-aligned so their last digits line up, just like a real bill.
Important Points to Remember
- Format:
%[flag][width][.precision]specifier. - Width = minimum spaces; never cuts a number.
-= left-align;0= pad with zeros.%.2f= 2 decimal places (rounded).%.3s= only 3 characters of a string.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
%2.f meaning "2 decimals" | Width 2, 0 decimals | Use %.2f |
Expecting %3d to cut 12345 | Prints 12345 in full | Width is only a minimum |
%-05d expecting zeros on the right | The 0 flag is ignored with - | Zeros only pad on the left |
| Mixing tabs and widths for columns | Columns don't line up | Use widths for every column |
Width and Precision Practice Questions
- What is field width in printf?
- What is precision for a float and for a string?
- What does
printf("[%6.1f]", 2.75);print? - What does
printf("[%-4d]", 7);print? - How do you print a roll number 7 as 007?
Show answers
- The minimum number of character spaces used to print a value.
- For a float: digits after the decimal point. For a string: maximum characters printed.
[ 2.8](2.75 rounds to 2.8, padded to width 6).[7 ]printf("%03d", 7);
Coding Exercise
Print a 3-row marksheet with columns Subject (left-aligned, width 12) and Marks (right-aligned, width 6): Maths 95, Physics 88, Chemistry 76.
Show solution
#include <stdio.h>
int main(void)
{
printf("%-12s%6s\n", "Subject", "Marks");
printf("%-12s%6d\n", "Maths", 95);
printf("%-12s%6d\n", "Physics", 88);
printf("%-12s%6d\n", "Chemistry", 76);
return 0;
}
Subject Marks Maths 95 Physics 88 Chemistry 76
Quick Quiz: Width and Precision (MCQ)
-
What does
%.2fdo?A) Width 2 B) 2 decimal places C) 2 digits total D) Prints 2 values
-
Which left-aligns a number in width 6?
A)
%6dB)%-6dC)%06dD)%.6d -
Output of
printf("%04d", 25);A) 25 B) 2500 C) 0025 D) 25
-
Output of
printf("%.2s", "India");A) India B) In C) ia D) Error
-
Output of
printf("%2d", 9876);A) 98 B) 76 C) 9876 D) Error
Show answers
- B: 2 decimal places.
- B:
%-6d - C: 0025
- B: In
- C: 9876 (width never cuts a number).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| General form | %[flag][width][.precision]specifier |
|---|---|
| Width | Minimum spaces; never cuts |
| Precision (float) | Digits after the point, rounded |
| Precision (string) | Maximum characters printed |
- flag | Left-align |
0 flag | Pad with zeros |
| Default alignment | Right |
Frequently asked questions
- What is the difference between %5.2f and %.2f?
- Both show 2 decimals;
%5.2falso uses a minimum width of 5. - Does field width cut a long number?
- No. Width is only a minimum; the full value is printed.
- How do you print a number with leading zeros?
- Use the 0 flag:
%05dprints 42 as 00042. - Does %.1f round or cut?
- It rounds: 2.66 prints as 2.7.
Remember
- Before the dot = width, after the dot = precision.
- Minus = left, zero = fill with zeros.
Next Topic
Next lesson: Common scanf() Problems and Fixes. You will learn why scanf sometimes "skips" an input, and how to fix it.