Usage Tips

Specifying fixed field widths is useful when you want to print columns of data. Because the default field width is just the width of the number, the repeated use of, say,

printf("%d %d %d\n", val1, val2, val3);

produces ragged columns if the numbers in a column had different sizes. For example, the output could look like the following:

12 234 1222
4 5 23
22334 2322 10001

(This assumes that the value of the variables has been changed between print statements.)

The output can be cleaned up by using a sufficiently large fixed field width. For example, using

printf("%9d %9d %9d\n", val1, val2, val3);

yields the following:

   12        234       1222
    4          5         23
22334       2322      10001

Leaving a blank between one conversion specification and the next ensures ...

Get C Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.