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);
would produce ragged columns if the numbers in a column had different sizes. For example, the output could look like this:
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);
would yield
   12        234       1222
    4          5         23
22334       2322      10001

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

Get C Primer Plus®, Third 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.