|
|
1.1 root 1: .TH PRINTF 3S
2: .CT 2 file_io
3: .SH NAME
4: printf, fprintf, sprintf, snprintf \(mi print formatted output
5: .SH SYNOPSIS
6: .nf
7: .B "#include <stdio.h>"
8: .PP
9: .B "int printf(char *format, ... );
10: .PP
11: .B "int fprintf (FILE *stream, char *format, ... );
12: .PP
13: .B "int sprintf (char *s, char *format, ... );
14: .PP
15: .B "int snprintf (char *s, int len, char *format, ... );
16: .fi
17: .SH DESCRIPTION
18: .I Printf
19: places output on the standard output stream
20: .IR stdout .
21: .I Fprintf
22: places output on the named output
23: .IR stream .
24: .I Sprintf
25: places output
26: followed by the null character
27: .RB ( \e0 ),
28: in consecutive bytes starting at
29: .IR s ;
30: it is the user's responsibility to ensure that
31: enough storage is available.
32: .I Snprintf
33: corresponds to
34: .IR sprintf
35: except that no more than
36: .IR len
37: bytes are placed into
38: .IR s .
39: Each function returns the number of characters
40: transmitted (not including the
41: .B \e0
42: in the case of
43: .IR sprintf ),
44: or
45: a negative value if an output error was encountered.
46: .PP
47: Each of these functions
48: converts, formats, and prints its
49: trailing arguments
50: under control of a
51: .IR format
52: string.
53: The
54: .I format
55: contains two types of objects:
56: plain characters, which are simply copied to the
57: output stream,
58: and conversion specifications,
59: each of which results in fetching of
60: zero or more
61: arguments.
62: The results are undefined if there are arguments of the
63: wrong type or too few
64: arguments for the format.
65: If the format is exhausted while
66: arguments remain, the excess
67: are ignored.
68: .PP
69: Each conversion specification is introduced by
70: the character
71: .BR % .
72: After the
73: .BR % ,
74: the following
75: appear in sequence:
76: .PP
77: .RS
78: Zero or more
79: .IR flags ,
80: which modify the meaning of
81: the conversion specification.
82: .PP
83: An optional decimal digit string specifying a minimum
84: .IR "field width" .
85: If the converted value has fewer characters
86: than the field width,
87: it is padded to the field width.
88: When the width specification begins with zero, padding is
89: with leading zeros.
90: Otherwise padding is with leading spaces (trailing spaces,
91: with the left-adjustment flag
92: .LR - ,
93: described below) to the field width.
94: .PP
95: A
96: .I precision\^
97: that gives
98: the minimum number of digits to appear for the
99: .BR d ,
100: .BR o ,
101: .BR u ,
102: .BR x ,
103: or
104: .B X
105: conversions,
106: the number of digits to appear after the
107: decimal point for the
108: .B e
109: and
110: .B f
111: conversions,
112: the maximum number of significant digits
113: for the
114: .B g
115: conversion,
116: or the maximum number of characters
117: to be printed from a string in
118: .B s
119: conversion.
120: The precision takes the form of a period
121: .RB ( \&. )
122: followed by a decimal digit string;
123: a null digit string is treated as zero.
124: .PP
125: An optional
126: .B l
127: (ell) specifying that a following
128: .BR d ,
129: .BR o ,
130: .BR u ,
131: .BR x ,
132: or
133: .B X
134: conversion character applies to a long
135: integer
136: .IR arg .
137: An
138: .B l
139: before any other conversion character is ignored.
140: .PP
141: A character that indicates the type of
142: conversion to be applied.
143: .RE
144: .PP
145: A field width or precision may be
146: indicated by an asterisk
147: .RB ( * )
148: or an exclamation point
149: .RB ( ! )
150: instead of a digit string.
151: In this case, an integer
152: .I arg\^
153: supplies
154: the field width or precision.
155: .PP
156: The flag characters and their meanings are:
157: .PD 0
158: .TP 10
159: .B \-
160: The result of the conversion is left-justified within the field.
161: .TP
162: .B +
163: The result of a signed
164: conversion always begins with a sign
165: .RB ( +
166: or
167: .BR - ).
168: .TP
169: blank
170: If the first character of a signed conversion is not a sign, a blank
171: is prefixed to the result.
172: This implies that if the blank and
173: .B +
174: flags both appear, the blank flag is ignored.
175: .TP
176: .B #
177: This flag specifies that the value is to be converted
178: to an ``alternate form.''
179: For
180: .BR c ,
181: .BR d ,
182: .BR s ,
183: and
184: .B u
185: conversions, the flag has no effect.
186: For
187: .B o
188: conversion, it increases the precision to force
189: the first digit of the result to be a zero.
190: For
191: .B x or X
192: conversion, a non-zero result has
193: .B 0x or 0X
194: prefixed to it.
195: For
196: .BR e ,
197: .BR E ,
198: .BR f ,
199: .BR g ,
200: and
201: .B G
202: conversions, the result always contains a decimal point,
203: even if no digits follow the point (normally, a decimal point
204: appears in the result of these conversions only if a digit
205: follows it).
206: For
207: .B g
208: and
209: .B G
210: conversions, trailing zeros are
211: .I not\^
212: be removed from the result
213: as they normally are.
214: .PD
215: .PP
216: The conversion characters
217: and their meanings are:
218: .PP
219: .PD 0
220: .TP 10
221: \fLd\fP,\fLo\fP,\fLu\fP,\fLx\fP,\fLX\fP
222: The integer
223: .I arg\^
224: is converted to signed decimal,
225: unsigned octal, decimal, or
226: hexadecimal notation
227: .RB ( x
228: and
229: .BR X ),
230: respectively;
231: the letters
232: .B abcdef
233: are used for
234: .B x
235: conversion and the letters
236: .B ABCDEF
237: for
238: .B X
239: conversion.
240: The precision specifies the minimum number of digits
241: to appear; if the value being converted can be represented
242: in fewer digits, it is expanded with leading zeros.
243: (For compatibility with other versions of
244: .IR printf ,
245: a field width with a leading zero
246: results in padding with leading zeros.
247: This does not imply an octal value for the field width.)
248: The default precision is 1.
249: The result of converting a zero value with a precision
250: of zero is a null string.
251: .TP
252: .BR f
253: The float or double
254: .I arg\^
255: is converted to decimal notation
256: in the style
257: [\fB-\fR]\fId\fB.\fIddd\fR,
258: where the number of digits after the decimal point
259: is equal to the precision specification.
260: If the precision
261: is missing,
262: six digits are output;
263: if the precision is explicitly
264: .LR 0 ,
265: no decimal point appears.
266: .TP
267: .BR e , E
268: The float or double
269: .I arg\^
270: is converted in the style
271: [\fB-\fR]\fId\fB.\fIddd\fBe\(+-\fIdd\fR,
272: where there is one digit before the decimal point and
273: the number of digits after it is equal to the
274: precision;
275: when the precision is missing,
276: six digits are produced;
277: if the precision is zero, no decimal point appears.
278: The
279: .B E
280: format code produces a number with
281: .B E
282: instead of
283: .B e
284: introducing the exponent.
285: The exponent always contains at least two digits.
286: .TP
287: .BR g , G
288: The float or double
289: .I arg\^
290: is printed in style
291: .BR f
292: or
293: .BR e
294: (or in style
295: .B E
296: in the case of a
297: .B G
298: format code),
299: with the precision specifying the number of significant digits.
300: The style used depends on the value converted:
301: style
302: .B e
303: is used only if the exponent resulting from
304: the conversion is less than -4
305: or greater than the precision.
306: Trailing zeros are removed from the result; a decimal point
307: appears only if it is followed by a digit.
308: .TP
309: .B c
310: The character
311: argument is printed.
312: .TP
313: .B s
314: The
315: argument is taken to be a string (character pointer)
316: and characters from the string are printed until
317: a null character
318: .RB ( \e0 )
319: is encountered or
320: the number of characters indicated by the precision
321: specification is reached.
322: If the precision is missing, it is taken to be infinite, so
323: all characters up to the first null character are printed.
324: A
325: zero
326: value for
327: the argument yields undefined results.
328: (For compatibility with other versions of
329: .IR printf ,
330: a field width with
331: a leading zero results in zero-padding the string instead
332: of blank-padding it.
333: This does not imply an octal value for the field width.)
334: .TP
335: .B %
336: Print a
337: .BR % ;
338: no argument is converted.
339: .PD
340: .PP
341: In no case does a non-existent or small field width
342: cause truncation of a field;
343: if the result of a conversion is wider than the field width,
344: the field is simply expanded to contain the conversion result.
345: Characters generated by
346: .I printf\^
347: and
348: .I fprintf\^
349: are printed as if
350: .IR putc
351: had been called; see
352: .IR getc (3).
353: .SH EXAMPLES
354: .TP
355: .L
356: printf("%s, %s %d, %d:%.2d", weekday, month, day, hour, min);
357: Print a date and time in the form `Sunday, July 3, 10:02',
358: where
359: .I weekday\^
360: and
361: .I month\^
362: are pointers to null-terminated strings.
363: .TP
364: .L
365: printf("pi = %.5f", 4*atan(1.0));
366: Print
367: .if n .I pi\^
368: .if t \(*p
369: to 5 decimal places.
370: .SH SEE ALSO
371: .IR ecvt (3),
372: .IR scanf (3),
373: .IR stdio (3),
374: .IR print (3)
375: .SH BUGS
376: The
377: .L !
378: indicator for field width is nonstandard.
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.