BSD 4_2 development
[unix-history] / usr / man / man3 / printf.3s
CommitLineData
c07b64e6
C
1.TH PRINTF 3S "1 April 1981"
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.PP
32.B #include <varargs.h>
33.br
34.B _doprnt(format, args, stream)
35.br
36.B char *format;
37.br
38.B va_list *args;
39.br
40.B FILE *stream;
41.SH DESCRIPTION
42.I Printf
43places output on the standard output stream
44.BR stdout .
45.I Fprintf
46places output on the named output
47.IR stream .
48.I Sprintf
49places `output' in the string
50.IR s ,
51followed by the character `\\0'.
52All of these routines work by calling the internal
53routine
54.B _doprnt,
55using the variable-length argument facilities of
56.IR varargs (3).
57.PP
58Each of these functions converts, formats, and prints its arguments after
59the first under control of the first argument.
60The first argument is a character string which contains two types of objects:
61plain characters, which are simply copied to the output stream,
62and conversion specifications, each of which causes conversion and printing
63of the next successive
64.I arg
65.IR printf .
66.PP
67Each conversion specification is introduced by the character
68.BR % .
69Following the
70.BR % ,
71there may be
72.TP
73.B \(bu
74an optional minus sign `\-' which specifies
75.I "left adjustment"
76of the converted value in the indicated field;
77.TP
78.B \(bu
79an optional digit string specifying a
80.I "field width;"
81if the converted value has fewer characters than the field width
82it will be blank-padded on the left (or right,
83if the left-adjustment indicator has been given) to make up the field width;
84if the field width begins with a zero,
85zero-padding will be done instead of blank-padding;
86.TP
87.B \(bu
88an optional period
89.RB ` . '
90which serves to separate the field width from the next digit string;
91.TP
92.B \(bu
93an optional digit string specifying a
94.I precision
95which specifies the number of digits to appear after the
96decimal point, for e- and f-conversion, or the maximum number of characters
97to be printed from a string;
98.TP
99.B \(bu
100an optional `#' character
101specifying that the value should be converted to an ``alternate form''.
102For
103.BR c ,
104.BR d ,
105.BR s ,
106and
107.BR u ,
108conversions, this option has no effect. For
109.B o
110conversions, the precision of the number is increased to force the first
111character of the output string to a zero. For
112.BR x ( X )
113conversion, a non-zero result has the string
114.BR 0x ( 0X )
115prepended to it. For
116.BR e ,
117.BR E ,
118.BR f ,
119.BR g ,
120and
121.BR G ,
122conversions, the result will always contain a decimal point, even if no
123digits follow the point (normally, a decimal point only appears in the
124results of those conversions if a digit follows the decimal point). For
125.B g
126and
127.B G
128conversions, trailing zeros are not removed from the result as they
129would otherwise be.
130.TP
131.B \(bu
132the character
133.B l
134specifying that a following
135.BR d ,
136.BR o ,
137.BR x ,
138or
139.B u
140corresponds to a long integer
141.IR arg .
142.TP
143.B \(bu
144a character which indicates the type of
145conversion to be applied.
146.PP
147A field width or precision may be `*' instead of a digit string.
148In this case an integer
149.I arg
150supplies
151the field width or precision.
152.PP
153The conversion characters
154and their meanings are
155.TP
156.B dox
157The integer
158.I arg
159is converted to decimal, octal, or
160hexadecimal notation respectively.
161.TP
162.B f
163The float or double
164.I arg
165is converted to decimal notation
166in the style `[\fB\-\fR]ddd.ddd'
167where the number of d's after the decimal point
168is equal to the precision specification
169for the argument.
170If the precision
171is missing,
1726 digits are given;
173if the precision is explicitly 0, no digits and
174no decimal point are printed.
175.TP
176.B e
177The float or double
178.I arg
179is converted in the style
180`[\fB\-\fR]d\fB.\fRddd\fBe\fR\(+-dd'
181where there is one digit before the decimal point and
182the number after is equal to the
183precision specification for the argument;
184when the precision is missing,
1856 digits are produced.
186.TP
187.B g
188The float or double
189.I arg
190is printed in style
191.BR d ,
192in style
193.BR f ,
194or in
195style
196.BR e ,
197whichever gives full precision in minimum space.
198.TP
199.B c
200The character
201.I arg
202is printed.
203.TP
204.B s
205.I Arg
206is taken to be a string (character pointer)
207and characters from the string are printed until
208a null character or until
209the number of characters indicated by the precision
210specification is reached;
211however if the precision is 0 or missing
212all characters up to a null are printed.
213.TP
214.B u
215The unsigned integer
216.I arg
217is converted to decimal
218and printed (the result will be in the
219range 0 through MAXUINT, where MAXUINT equals 4294967295 on a VAX-11
220and 65535 on a PDP-11).
221.TP
222.B %
223Print a `%'; no argument is converted.
224.PP
225In no case does a non-existent or small field width
226cause truncation of a field;
227padding takes place only if the specified field
228width exceeds the actual width.
229Characters generated by
230.I printf
231are printed by
232.IR putc (3S).
233.PP
234.B Examples
235.br
236To print a date and time in the form `Sunday, July 3, 10:02',
237where
238.I weekday
239and
240.I month
241are pointers to null-terminated strings:
242.RS
243.HP
244.nh
245printf("%s, %s %d, %02d:%02d", weekday, month, day, hour, min);
246.RE
247.hy
248.PP
249To print
250.if n pi
251.if t \(*p
252to 5 decimals:
253.IP
254printf("pi = %.5f", 4*atan(1.0));
255.SH "SEE ALSO"
256putc(3S),
257scanf(3S),
258ecvt(3)
259.SH BUGS
260Very wide fields (>128 characters) fail.