O'Reilly logo

C Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required