The power of the printf
function, relative to the output operator,
<<
, is that you can control how much space each argument value
occupies by adding a number to the print specification. The print
specification the corresponding argument value, a string, along with extra spaces, if
necessary, on the left
. Similarly, eight characters using the corresponding argument value, an integer, along
with spaces, if necessary, on the left
.
Consider, for example, the following printf
call:
printf ("
The result includes the argument values, along with the spaces required to meet the stipulated minimum number of characters:
*----- Spaces preceding first argument value start here | | *----- First argument-value characters start here | | | | *----- Spaces preceding second argument value start here | | | | | | *----- Second argument-value characters start here | | | | v v v v --- ---- box 4703
The stipulated minimum number of characters is called the field width. The spaces used to augment the space taken by the argument values are called padding characters.