How to Program in C for the Beginner: Using a Field Width to Print Integers

How to Program in C for the Beginner: Using a Field Width to Print Integers
Page content

Printing an integer using a “field width”

We have seen that we can print an integer value by specifying the value (either by a variable or an expression) in a printf statement. When we do so, C prints the value using as many “print columns” as needed. For instance, if the value is 782, it is printed using 3 print columns since 782 has 3 digits. If the value is -2345, it is printed using 5 print columns (one for the minus sign).

While this is usually sufficient for most purposes, there are times when it is useful to be able to tell C how many print columns to use. For example, if we want to print the value of n in 5 print columns, we can do this by specifying a field width of 5, as in:

printf("%5d", n);

Instead of the specification %d, we now use %5d. The field width is placed between % and d. The value of n is printed “in a field width of 5”.

Suppose n is 279; there are 3 digits to print so 3 print columns are needed. Since the field width is 5, the number 279 is printed with 2 spaces before it, thus: ◊◊279 (◊ denotes a space). We also say “printed with 2 leading blanks/spaces” and “printed padded on the left with 2 blanks/spaces”.

A more technical way of saying this is “n is printed right-justified in a field width of 5”. “Right-justify” means that the number is placed as far right as possible in the field and spaces added in front of it to make up the field width. If the number is placed as far left as possible and spaces are added after it to make up the field width, the number is left-justified. For example, 279◊◊ is left-justified in a field width of 5.

The minus sign can be used to specify left-justification; %-wd will print a value left-justified in a field width of w. For example, to print an integer value left-justified in field width of 5, we use %-5d.

For another example, suppose n is -7 and the field width is 5. Printing n requires two print columns (one for - and one for 7); since the field width is 5, it is printed with 3 leading spaces, thus: ◊◊◊-7.

You may ask, what will happen if the field width is too small? Suppose the value to be printed is 23456 and the field width is 3. Printing this value requires 5 columns which is greater than the field width 3. In this case, C ignores the field width and simply prints the value using as many columns as needed (5, in this example).

In general, suppose the integer value v is printed with the format specification %wd where w is an integer, and suppose n columns are needed to print v. There are 2 cases to consider:

• If n is less than w (the field width is bigger), the value is padded on the left with (w ‑ n) spaces. For example, if w is 7 and v is -345 so that n is 4, the number is padded on the left with (7-4) = 3 spaces and printed as ◊◊◊-345.

• If n is greater than or equal to w (field width is the same or smaller), the value is printed using n print columns. In this case, the field width is ignored.

Field Width Example

A field width is useful when we want to line up numbers one below the other. Suppose we have three int variables a, b and c with values 9876, -3 and 501, respectively. The statements

printf("%d\n", a); printf("%d\n", b); printf("%d\n", c);

will print

9876 -3 501

Each number is printed using just the number of columns required. Since this varies from one number to the next, they do not line up. If we want to, we could get the numbers lined up using a field width of 5, say. The statements

printf("%5d\n", a); printf("%5d\n", b); printf("%5d\n", c);

will print ( denotes a space)

◊9876 ◊◊◊-3 ◊◊501

all nicely lined up.

As a matter of interest, we don’t really need 3 printf statements. We can replace the last 3 printf statements with

printf("%5d\n%5d\n%5d\n", a, b, c);

Each \n forces the following output onto a new line.

References

C Programming – A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int