1PRINTF(3S) PRINTF(3S)
2
3
4
6 printf, fprintf, sprintf, vfprintf, vsprintf - formatted output conver‐
7 sion
8
10 #include <stdio.h>
11
12 char *printf(format [, arg ] ... )
13 char *format;
14
15 char *fprintf(stream, format [, arg ] ... )
16 FILE *stream;
17 char *format;
18
19 int sprintf(s, format [, arg ] ... )
20 char *s, *format;
21
22 #include <varargs.h>
23 char *vprintf(format, args)
24 char *format;
25 va_list args;
26
27 char *vfprintf(stream, format, args)
28 FILE *stream;
29 char *format;
30 va_list args;
31
32 int vsprintf(s, format, args)
33 char *s, *format;
34 va_list args;
35
37 Printf places output on the standard output stream stdout. Fprintf
38 places output on the named output stream. Sprintf places `output' in
39 the string s, followed by the character `\0'. Alternate forms, in
40 which the arguments have already been captured using the variable-
41 length argument facilities of varargs(3), are available under the names
42 vprintf, vfprintf, and vsprintf.
43
44 Each of these functions converts, formats, and prints its arguments
45 after the first under control of the first argument. The first argu‐
46 ment is a character string which contains two types of objects: plain
47 characters, which are simply copied to the output stream, and conver‐
48 sion specifications, each of which causes conversion and printing of
49 the next successive arg printf.
50
51 Each conversion specification is introduced by the character %. The
52 remainder of the conversion specification includes in the following
53 order
54
55 · a minus sign `-' which specifies left adjustment of the con‐
56 verted value in the indicated field;
57
58 · an optional digit string specifying a field width; if the con‐
59 verted value has fewer characters than the field width it will
60 be blank-padded on the left (or right, if the left-adjustment
61 indicator has been given) to make up the field width; if the
62 field width begins with a zero, zero-padding will be done
63 instead of blank-padding;
64
65 · an optional period, followed by an optional digit string giving
66 a precision which specifies the number of digits to appear after
67 the decimal point, for e- and f-conversion, or the maximum num‐
68 ber of characters to be printed from a string;
69
70 · the character l specifying that a following d, o, x, or u corre‐
71 sponds to a long integer arg;
72
73 · a character which indicates the type of conversion to be
74 applied.
75
76 A field width or precision may be `*' instead of a digit string. In
77 this case an integer arg supplies the field width or precision.
78
79 The conversion characters and their meanings are
80
81 dox The integer arg is converted to signed decimal, unsigned octal,
82 or unsigned hexadecimal notation respectively.
83
84 f The float or double arg is converted to decimal notation in the
85 style `[-]ddd.ddd' where the number of d's after the decimal
86 point is equal to the precision specification for the argument.
87 If the precision is missing, 6 digits are given; if the preci‐
88 sion is explicitly 0, no digits and no decimal point are
89 printed.
90
91 e The float or double arg is converted in the style `[-]d.ddde±dd'
92 where there is one digit before the decimal point and the number
93 after is equal to the precision specification for the argument;
94 when the precision is missing, 6 digits are produced.
95
96 g The float or double arg is printed in style d, in style f, or in
97 style e, whichever gives full precision in minimum space.
98
99 c The character arg is printed.
100
101 s Arg is taken to be a string (character pointer) and characters
102 from the string are printed until a null character or until the
103 number of characters indicated by the precision specification is
104 reached; however if the precision is 0 or missing all characters
105 up to a null are printed.
106
107 u The unsigned integer arg is converted to decimal and printed
108 (the result will be in the range 0 through MAXUINT, where
109 MAXUINT equals 4294967295 on a VAX-11 and 65535 on a PDP-11).
110
111 % Print a `%'; no argument is converted.
112
113 In no case does a non-existent or small field width cause truncation of
114 a field; padding takes place only if the specified field width exceeds
115 the actual width. Characters generated by printf are printed as by
116 putc(3S).
117
119 The functions all return the number of characters printed, or -1 if an
120 error occurred.
121
123 To print a date and time in the form `Sunday, July 3, 10:02', where
124 weekday and month are pointers to null-terminated strings:
125
126 printf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);
127
128 To print pi to 5 decimals:
129
130 printf("pi = %.5f", 4*atan(1.0));
131
133 putc(3S), scanf(3S)
134
136 Very wide fields (>300 characters) fail.
137
138 Only sprintf and vsprintf return a count of characters transferred.
139
140 The functions still supports %D, %O, %U and %X. Do not use these for‐
141 mats, as they will be disappearing real soon now.
142
143
144
1457th Edition August 10, 1988 PRINTF(3S)