1PRINTF(3S) PRINTF(3S)
2
3
4
6 printf, fprintf, sprintf - formatted output conversion
7
9 #include <stdio.h>
10
11 printf(format [, arg ] ... )
12 char *format;
13
14 fprintf(stream, format [, arg ] ... )
15 FILE *stream;
16 char *format;
17
18 sprintf(s, format [, arg ] ... )
19 char *s, format;
20
22 Printf places output on the standard output stream stdout. Fprintf
23 places output on the named output stream. Sprintf places `output' in
24 the string s, followed by the character `\0'.
25
26 Each of these functions converts, formats, and prints its arguments
27 after the first under control of the first argument. The first argu‐
28 ment is a character string which contains two types of objects: plain
29 characters, which are simply copied to the output stream, and conver‐
30 sion specifications, each of which causes conversion and printing of
31 the next successive arg printf.
32
33 Each conversion specification is introduced by the character %. Fol‐
34 lowing the %, there may be
35
36 - an optional minus sign `-' which specifies left adjustment of
37 the converted value in the indicated field;
38
39 - an optional digit string specifying a field width; if the con‐
40 verted value has fewer characters than the field width it will
41 be blank-padded on the left (or right, if the left-adjustment
42 indicator has been given) to make up the field width; if the
43 field width begins with a zero, zero-padding will be done
44 instead of blank-padding;
45
46 - an optional period `.' which serves to separate the field width
47 from the next digit string;
48
49 - an optional digit string specifying a precision which specifies
50 the number of digits to appear after the decimal point, for e-
51 and f-conversion, or the maximum number of characters to be
52 printed from a string;
53
54 - the character l specifying that a following d, o, x, or u corre‐
55 sponds to a long integer arg. (A capitalized conversion code
56 accomplishes the same thing.)
57
58 - a character which indicates the type of conversion to be
59 applied.
60
61 A field width or precision may be `*' instead of a digit string. In
62 this case an integer arg supplies the field width or precision.
63
64 The conversion characters and their meanings are
65
66 dox The integer arg is converted to decimal, octal, or hexadecimal
67 notation respectively.
68
69 f The float or double arg is converted to decimal notation in the
70 style `[-]ddd.ddd' where the number of d's after the decimal
71 point is equal to the precision specification for the argument.
72 If the precision is missing, 6 digits are given; if the preci‐
73 sion is explicitly 0, no digits and no decimal point are
74 printed.
75
76 e The float or double arg is converted in the style `[-]d.ddde±dd'
77 where there is one digit before the decimal point and the number
78 after is equal to the precision specification for the argument;
79 when the precision is missing, 6 digits are produced.
80
81 g The float or double arg is printed in style d, in style f, or in
82 style e, whichever gives full precision in minimum space.
83
84 c The character arg is printed. Null characters are ignored.
85
86 s Arg is taken to be a string (character pointer) and characters
87 from the string are printed until a null character or until the
88 number of characters indicated by the precision specification is
89 reached; however if the precision is 0 or missing all characters
90 up to a null are printed.
91
92 u The unsigned integer arg is converted to decimal and printed
93 (the result will be in the range 0 to 65535).
94
95 % Print a `%'; no argument is converted.
96
97 In no case does a non-existent or small field width cause truncation of
98 a field; padding takes place only if the specified field width exceeds
99 the actual width. Characters generated by printf are printed by
100 putc(3).
101
102 Examples
103 To print a date and time in the form `Sunday, July 3, 10:02', where
104 weekday and month are pointers to null-terminated strings:
105
106 printf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);
107
108 To print pi to 5 decimals:
109
110 printf("pi = %.5f", 4*atan(1.0));
111
113 putc(3), scanf(3), ecvt(3)
114
116 Very wide fields (>128 characters) fail.
117
118
119
120 PRINTF(3S)