C’s rich assortment of output formatting controls makes it easy to create a neatly printed graph, report, or table. The two main functions that accomplish this formatted output are printf( ) and the file equivalent form, fprintf( ). These functions can use any of the format specifiers shown in Table 11-2. The format specification uses the following form:
Table 11-2: Format Specifiers for printf() and fprintf() Functions
TYPE FIELD: Character
Type
Format of Output
c
int or wint_t
printf( ) means a single-byte character.
wprintf( ) means a wide character.
C
int or wint_t
printf( ) means a wide character.
wprintf( ) means a single-byte character.
d
int
Signed decimal integer.
e
double
Signed number of the form [-]d.ddd e [sign]dddd.
Here, d is a single decimal digit, ddd is one or more decimal digits, dddd is exactly four decimal digits, and the sign is a + or -.
E
double
Same as e, except “E” is in front of exponent.
f
double
Signed number of the form [-]ddd.ddd. Here, ddd is one or more decimal digits. The number of digits after the decimal point depends upon the precision.
g
double
Signed number in f or e format. The most compact format is used. No trailing zeroes. No decimal point if no digits follow it.
G
double
Same as g format, except “E” is in front of exponent.
i
int
Signed decimal integer.
n
Pointer to integer
The # of characters written to the stream or buffer.
Left-align the result (right alignment is the default).
+
Use a leading sign (+ or -) if number is a signed type (sign used with negative number only is the default).
0
When width has 0 prefix, zeroes will be added until the minimum width is reached (no padding is the default).
blank (‘ ‘)
Output is prefixed with a blank. If positive and signed, the blank will be ignored (no appearing blanks is the default).
#
Prefixes nonzero output value with 0, 0x, or 0X (no appearing blank is the default).
.
For e, E, or f formats, a # makes the output value contain a decimal point in all cases (point appears only if digits follow is the default).
%[flags] [width] [.precision] [{h | l | L}]type
The field of the specification is a character or a number that gives a format option. The simplest form can be just a percent sign and a type. For example, %f. type is used to determine if the argument is to be interpreted as a character, a string, or a number. flags are used to control the printing of signs, blanks, decimal points, radix of output, and so on. width refers to the minimum number of characters to print. precision refers to the maximum number of characters that are printed for the output. h | l | L are optional prefixes for giving the argument size.
Using printf( ) and fprintf( )
The following example program defines four variable types: character, array-of-characters, integer, and real. It then demonstrates how to use the appropriate format controls on each variable. The source code has been heavily commented and output line numbering has been included to make associating the output generated with the statement that created it as simple as possible:
/* * printf.c * A C program demonstrating advanced conversions and * formatting * Copyright (c) Bill/Chris H. Pappas and William H. Murray, 1998 */
#include <stdio.h>
void main( ) { char c = ‘A’, psz1[] = “In making a living today many no ”, psz2[] = “longer leave any room for life.”; int iln = 0, ivalue = 1234; double dPi = 3.14159265;
/* conversions */
/* print the c */ printf(“\n[%2d] %c”,++iln,c);
/* print the ASCII code for c */ printf(“\n[%2d] %d”,++iln,c);
/* print character with ASCII 90 */ printf(“\n[%2d] %c”,++iln,90);
/* print ivalue as octal value */ printf(“\n[%2d] %o”,++iln,ivalue);
The output generated by the program looks like this:
[ 1] A [ 2] 65 [ 3] Z [ 4] 2322 [ 5] 4d2 [ 6] 4D2 [ 7] A [ 8] A [ 9] A [10] In making a living today many no [11] longer leave any room for life. [12] In making a living today many no [13] In making a living today many no [14] longer leave any room for life. [15] 1234 [16] +1234 [17] 1234 [18] 1234 [19] 1234 [20] 0000001234 [21] 3.141593 [22] 3.141593 [23] 0000000000003.141593 [24] 3.141593 [25] In making a living [26] In [27] In [28] In [29] In mak [30] 3.14159265 [31] 3.14 [32] 3.1416 [33] 3.1416 [34] 3.14e+000
You can neatly format your application’s output by studying the preceding example and selecting those combinations that apply to your program’s data types.