Previous: 8.2 Formatted Input/Output
Up: 8.2 Formatted Input/Output
Next: 8.2.2 Formatted Input: scanf()
Previous Page: 8.2 Formatted Input/Output
Next Page: 8.2.2 Formatted Input: scanf()

8.2.1 Formatted Output: printf()

As we have seen, printf() expects arguments giving a format string and values to be printed. The printf() prototype, in stdio.h, is:

int printf(char *, ...);
The first argument of printf() is the format string (we will see what the above type declaration means in the next chapter). The number of remaining arguments depends on the number of conversion specifiers in the format string. In C, an ellipsis, i.e. ``...'', is used to indicate an arbitrary number of arguments. The return value of printf() is an int giving the number of bytes output, if successful; otherwise it returns EOF. This information from printf() is not generally very useful, and we often simply ignore the return value.

The function, printf(), converts, formats, and prints its arguments on the standard output using the conversion specifications given in the format string. The format string is made up of two kinds of characters: regular characters, which are simply copied to the output, and conversion specification characters. A conversion specification indicates how the corresponding argument value is to be converted and formatted before being printed. The number of conversion specifications in the format string must match exactly the number of arguments that follow; otherwise, the results are undefined. The data type of the argument should also match the data type it will be converted to; for example, integral types for decimal integer formats, float or double types for floating point or exponential formats, and so on. If the proper type is not used, the conversion is performed anyway assuming correct data types and the results can be very strange and unexpected. Of course, character values are integral types; so characters can be converted to ASCII integer values for printing, or printed as characters. We have already seen most of the conversion characters. Table 8.1 gives a complete list with their meanings.

We will discuss some examples, given the following declarations and initializations:

int i;
    char c;
    float f1;
    double d1;
    char *s;
    long x;

i = 33; c = 'e'; f1 = 12345.00 d1 = 12345.00 s = "This is a test"; x = 123456789;

Different conversion characters may be used to print the values of these variables. The space used to print a value is called the field, and by default, is exactly the space needed to print the value. We show examples of conversion characters and default output below:

So far, we have used very simple conversion specifiers, such as %d, %f, and %c. A complete conversion specification starts with the character % and ends with a conversion character. Between these two characters, special format characters may be used which can specify justification, field width, field separation, precision, and length modification. The characters that follow the % character and precede the conversion characters are called format characters. All format characters are optional, and if they are absent their default values are assumed. (We will indicate the default value in each case below). The syntax of a complete conversion specifier is:

where X is one of the conversion characters from Table 8.1. The other format characters must appear in the order specified above and represent the following formatting information: (the corresponding characters are shown in parentheses).

Some examples of format specifications using the previous variable types and values are shown below, where the field width is shown between the markers, '174 and '174.
Conversion    Variable   Output

Field Pos. 01234567890123456789 ___________________________________________________________ %10d i | 33| %-10d i |33 | | | %10f f1 |12345.000000 %-10f f1 |12345.000000 %20.2f f1 | 12345.00| %-20.2f f1 |12345.00 | %5c c | E| %-5c c |E |

%10s s |This is a test %20s s | This is a test| %-20s s |This is a test | %20.10s s | This is a |

%ld x |123456789 %-12ld x |123456789 |



Previous: 8.2 Formatted Input/Output
Up: 8.2 Formatted Input/Output
Next: 8.2.2 Formatted Input: scanf()
Previous Page: 8.2 Formatted Input/Output
Next Page: 8.2.2 Formatted Input: scanf()

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:15:23 HST 1994