BSD 3 development
[unix-history] / usr / man / man3 / printf.3s
CommitLineData
e6817382
BJ
1.TH PRINTF 3S
2.SH NAME
3printf, fprintf, sprintf \- formatted output conversion
4.SH SYNOPSIS
5.B #include <stdio.h>
6.PP
7.B printf(format
8.RB [ ,
9arg ] ...
10.B )
11.br
12.B char *format;
13.PP
14.B fprintf(stream, format
15.RB [ ,
16arg ] ...
17.B )
18.br
19.SM
20.B FILE
21.B *stream;
22.br
23.B char *format;
24.PP
25.B sprintf(s, format
26.RB [ ,
27arg ] ...
28.B )
29.br
30.B char *s, format;
31.SH DESCRIPTION
32.I Printf
33places output on the standard output stream
34.IR stdout .
35.I Fprintf
36places output on the named output
37.IR stream .
38.I Sprintf
39places `output' in the string
40.I s,
41followed by the character `\\0'.
42.PP
43Each of these functions
44converts, formats, and prints its arguments after the first
45under control of the first argument.
46The first argument is a character string
47which contains
48two types of objects:
49plain characters, which are simply copied to the
50output stream,
51and conversion specifications,
52each of which causes conversion and printing
53of the next successive
54.I arg
55.IR printf .
56.PP
57Each conversion specification is introduced by
58the character
59.BR % .
60Following the
61.BR % ,
62there may be
63.TP
64\-
65an optional minus sign `\-' which specifies
66.I "left adjustment"
67of the converted value
68in the
69indicated field;
70.TP
71\-
72an optional digit string specifying a
73.I "field width;"
74if the converted value has fewer characters
75than the field width
76it will be blank-padded on the left (or right,
77if the left-adjustment indicator has been
78given) to make up the field width;
79if the field width begins with a zero,
80zero-padding will be done instead of blank-padding;
81.TP
82\-
83an optional period
84.RB ` . '
85which serves to
86separate the field width from the
87next digit string;
88.TP
89\-
90an optional digit string
91specifying a
92.I precision
93which specifies
94the number of digits to appear after the
95decimal point, for e- and f-conversion,
96or the maximum number of characters
97to be printed from a string;
98.TP
99\-
100the character
101.B l
102specifying that a following
103.BR d ,
104.BR o ,
105.BR x ,
106or
107.B u
108corresponds to a long integer
109.I arg.
110(A capitalized conversion code accomplishes
111the same thing.)
112.TP
113\-
114a character which indicates the type of
115conversion to be applied.
116.PP
117A field width or precision may be `*' instead of a digit string.
118In this case an integer
119.I arg
120supplies
121the field width or precision.
122.PP
123The conversion characters
124and their meanings are
125.TP
126.B dox
127The integer
128.I arg
129is converted to decimal, octal, or
130hexadecimal notation respectively.
131.TP
132.B f
133The float or double
134.I arg
135is converted to decimal notation
136in the style `[\fB\-\fR]ddd.ddd'
137where the number of d's after the decimal point
138is equal to the precision specification
139for the argument.
140If the precision
141is missing,
1426 digits are given;
143if the precision is explicitly 0, no digits and
144no decimal point are printed.
145.TP
146.B e
147The float or double
148.I arg
149is converted in the style
150`[\fB\-\fR]d\fB.\fRddd\fBe\fR\(+-dd'
151where there is one digit before the decimal point and
152the number after is equal to the
153precision specification for the argument;
154when the precision is missing,
1556 digits are produced.
156.TP
157.B g
158The float or double
159.I arg
160is printed in style
161.BR d ,
162in style
163.BR f ,
164or in
165style
166.BR e ,
167whichever gives full precision in minimum space.
168.TP
169.B c
170The character
171.I arg
172is printed.
173Null characters are ignored.
174.TP
175.B s
176.I Arg
177is taken to be a string (character pointer)
178and characters from the string are printed until
179a null character or until
180the number of characters indicated by the precision
181specification is reached;
182however if the precision is 0 or missing
183all characters up to a null are printed.
184.TP
185.B u
186The unsigned integer
187.I arg
188is converted to decimal
189and printed (the result will be in the
190range 0 through MAXUINT, where MAXUINT equals 4294967295 on a VAX-11
191and 65536 on a PDP-11).
192.TP
193.B %
194Print a `%'; no argument is converted.
195.PP
196In no case does a non-existent or small field width
197cause truncation of a field;
198padding takes place only if the specified field
199width exceeds the actual width.
200Characters generated by
201.I printf
202are printed by
203.IR putc (3).
204.PP
205.B Examples
206.br
207To print a date and time in the form `Sunday, July 3, 10:02',
208where
209.I weekday
210and
211.I month
212are pointers to null-terminated strings:
213.RS
214.HP
215.nh
216printf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);
217.RE
218.hy
219.PP
220To print
221.if n pi
222.if t \(*p
223to 5 decimals:
224.IP
225printf("pi = %.5f", 4*atan(1.0));
226.SH "SEE ALSO"
227putc(3),
228scanf(3),
229ecvt(3)
230.SH BUGS
231Very wide fields (>128 characters) fail.