The printf function allows the values of argument expressions to be printed in various formats. A large number of conversion characters are provided for printing expressions of different types. Also, the possibility of using several optional fields along with these conversion characters makes printf a very powerful and complicated function.
For a value being printed in a printf statement, we can specify the minimum fieldwidth, precision, justification (left or right), etc. using the format given below.
| Conversion character|
(d f c s, etc.)
When minimum field-width is not specified in a conversion specification, the corresponding argument is printed using as many character positions as necessary. However, if it is specified, the argument is printed in a field at least that wide, using additional spaces as necessary. If the field-width specified is more than what is required, the output values are right justified in the output field, unless the ·- · flag is used which causes the values to be left justified. If the specified field-width is insufficient for the value being printed, additional character positions are used as necessary.
When printing floating point numbers, precision specifies the minimum number of digits to be printed after the decimal point. If a number being printed has fewer digits after the decimal point than specified, it is padded with zeros. However, if it has more digits after the decimal point, its value is rounded to the specified digit. Thus, the conversion specification %8.4f causes numbers 100.12 and 123.12345 to be printed as 100.1200 and 123.1235, respectively. Note that. if we omit precision in the conversion specification, a default precision of six digits is used. In this section, we use the precision field only for floating point numbers.
Example Formatted Output
a) Using minimum field-width and precision
Consider the printf statements given below that print value of different types by specifying · minimum field-width for each data value and the precision for the floating values:
printf (“%10d\n”, 12);
printf(“%10.7f %10.4f\n”, 1.23456789, -1.2);
The output of these statements is given below.
Observe that the values are printed right justified in field-width of 10. Also observe that the first floating number is rounded to the seventh decimal place, whereas the second one is printed using additional zeros.
b) Effect of specifying insufficient field-width
printf(“%4s %4d %4f\n”, “Monday”, 12345, 1.2);
This statement uses a field-width of four for each constant to be printed. Note that this fieldwidth is not sufficient for any of the constants, including 1.2, as the default precision for a floating point number is six digits. Thus, the values are printed using as many character positions as required. The output is given below.
Monday 12345 1.200000
c) Printing tabular output specifying field-width and alignment
This example illustrates how we can use the formatting features of printf function to print data properly aligned in a tabular manner.
printf(“%-10s %6d %12.3f %4c\n”, “Monday”, 5, 2.1234, ‘A’);
printf(“%-10s %6d %12.3f %4c\n”, “Tuesday”, 16, 17.5126, ‘B’);
printf(“%-10s %6d %12.3f %4c\n”, “Wednesday”, 175, 120.25, ‘C’);
This code has three similar printf statements, each used to print four values using an identical format. Note that the minimum field-widths specified in the conversion specifications are more than what is required. Thus, the values will be printed right justified unless the ‘ – ‘ flag is used.
As the strings are printed using “-10s” conversion specification, each string is printed left justified in a field-width of ten characters, whereas the values of other arguments are printed right justified in respective fields. The floating numbers are printed using three decimal places after the decimal point. The output is given below.
Monday 5 2.123 A
Tuesday 16 17.513 B
Wednesday 175 120.250 C