Skip to content
StudyFame

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: %5d and %-5d.
  • Padding with zeros: %05d.
  • Decimal places: %.2f and %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.

FormatMeaningValueOutput
%5dWidth 5, right-aligned42[ 42]
%-5dWidth 5, left-aligned42[42 ]
%05dWidth 5, padded with zeros42[00042]
%.2f2 decimal places3.14159[3.14]
%8.2fWidth 8, 2 decimal places3.14159[ 3.14]
%.3sAt 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: %2d with 12345 prints 12345.
  • Without a minus sign, values are right-aligned (spaces on the left).
  • Precision on a float rounds the value: %.1f with 2.66 prints 2.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

  1. %5d: 42 has 2 digits, so 3 spaces are added on the left to make 5.
  2. %-5d: the minus moves 42 to the left; 3 spaces go on the right.
  3. %05d: the 0 flag fills the empty places with zeros instead of spaces.
  4. %.2f: rounds 3.14159 to 2 decimal places: 3.14.
  5. %8.2f: "3.14" is 4 characters; 4 spaces are added to make 8.
  6. %.3s: prints only the first 3 characters of "Hello".

How Width and Precision Work

  1. printf first converts the value to text using the precision (e.g. 3.14159 → "3.14").
  2. Then it compares the text length with the width.
  3. If the text is shorter, it adds spaces (or zeros) to reach the width, on the left by default or on the right with -.
  4. 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

MistakeWhat happensFix
%2.f meaning "2 decimals"Width 2, 0 decimalsUse %.2f
Expecting %3d to cut 12345Prints 12345 in fullWidth is only a minimum
%-05d expecting zeros on the rightThe 0 flag is ignored with -Zeros only pad on the left
Mixing tabs and widths for columnsColumns don't line upUse widths for every column

Width and Precision Practice Questions

  1. What is field width in printf?
  2. What is precision for a float and for a string?
  3. What does printf("[%6.1f]", 2.75); print?
  4. What does printf("[%-4d]", 7); print?
  5. How do you print a roll number 7 as 007?
Show answers
  1. The minimum number of character spaces used to print a value.
  2. For a float: digits after the decimal point. For a string: maximum characters printed.
  3. [ 2.8] (2.75 rounds to 2.8, padded to width 6).
  4. [7 ]
  5. 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)

  1. What does %.2f do?

    A) Width 2   B) 2 decimal places   C) 2 digits total   D) Prints 2 values

  2. Which left-aligns a number in width 6?

    A) %6d   B) %-6d   C) %06d   D) %.6d

  3. Output of printf("%04d", 25);

    A) 25   B) 2500   C) 0025   D)   25

  4. Output of printf("%.2s", "India");

    A) India   B) In   C) ia   D) Error

  5. Output of printf("%2d", 9876);

    A) 98   B) 76   C) 9876   D) Error

Show answers
  1. B: 2 decimal places.
  2. B: %-6d
  3. C: 0025
  4. B: In
  5. 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
WidthMinimum spaces; never cuts
Precision (float)Digits after the point, rounded
Precision (string)Maximum characters printed
- flagLeft-align
0 flagPad with zeros
Default alignmentRight

Frequently asked questions

What is the difference between %5.2f and %.2f?
Both show 2 decimals; %5.2f also 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: %05d prints 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.