Home Segments Index Top Previous Next

536: Mainline

You can, if you want, display neater reports by telling printf how much space each argument should occupy. You supply this space information by adding a number to the print specification. The print specification %6s, for example, means display at least six characters using the corresponding argument value, a string, along with extra spaces, if necessary, on the left. Similarly, %8i means display at least eight characters using the corresponding argument value, an integer, along with spaces, if necessary, on the left.

Consider, for example, the following statement:

printf ("%6s%8i\n", "Food", 300); 

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 
  Food     300 

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.