4660 update man pages for lofi in a zone
[unleashed.git] / usr / src / man / man3c / printf.3c
blob915db7d9ca9d5492d2d8ffe0bf7d56f84f20a2f3
1 '\" te
2 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
3 .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved.
4 .\" Copyright 1989 AT&T
5 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
6 .\" Portions Copyright (c) 1997, The Open Group. All Rights Reserved.
7 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at  http://www.opengroup.org/bookstore/.
8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
9 .\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
10 .\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
11 .\"  This notice shall appear on any product containing this material.
12 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
13 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
14 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
15 .TH PRINTF 3C "Jan 7, 2009"
16 .SH NAME
17 printf, fprintf, sprintf, snprintf, asprintf \- print formatted output
18 .SH SYNOPSIS
19 .LP
20 .nf
21 #include <stdio.h>
23 \fBint\fR \fBprintf\fR(\fBconst char *restrict\fR \fIformat\fR,
24      \fB/*\fR \fIargs\fR*/ ...);
25 .fi
27 .LP
28 .nf
29 \fBint\fR \fBfprintf\fR(\fBFILE *restrict\fR \fIstream\fR, \fBconst char *restrict\fR \fIformat\fR,
30      \fB/*\fR \fIargs\fR*/ ...);
31 .fi
33 .LP
34 .nf
35 \fBint\fR \fBsprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBconst char *restrict\fR \fIformat\fR,
36      \fB/*\fR \fIargs\fR*/ ...);
37 .fi
39 .LP
40 .nf
41 \fBint\fR \fBsnprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBsize_t\fR \fIn\fR,
42      \fBconst char *restrict\fR \fIformat\fR, \fB/*\fR \fIargs\fR*/ ...);
43 .fi
45 .LP
46 .nf
47 \fBint\fR \fBasprintf\fR(\fBchar **\fR \fIret\fR, \fBconst char *restrict\fR \fIformat\fR,
48      \fB/*\fR \fIargs\fR*/ ...);
49 .fi
51 .SH DESCRIPTION
52 .sp
53 .LP
54 The \fBprintf()\fR function places output on the standard output stream
55 \fBstdout\fR.
56 .sp
57 .LP
58 The \fBfprintf()\fR function places output on on the named output stream
59 \fIstream\fR.
60 .sp
61 .LP
62 The \fBsprintf()\fR function places output, followed by the null byte
63 (\fB\e0\fR), in consecutive bytes starting at \fIs\fR; it is the user's
64 responsibility to ensure that enough storage is available.
65 .sp
66 .LP
67 The \fBsnprintf()\fR function is identical to \fBsprintf()\fR with the addition
68 of the argument \fIn\fR, which specifies the size of the buffer referred to by
69 \fIs\fR. If \fIn\fR is 0, nothing is written and \fIs\fR can be a null pointer.
70 Otherwise, output bytes beyond the \fIn\fR-1st are discarded instead of being
71 written to the array and a null byte is written at the end of the bytes
72 actually written into the array.
73 .sp
74 .LP
75 The \fBasprintf()\fR function is the same as the \fBsprintf()\fR function
76 except that it returns, in the \fIret\fR argument, a pointer to a buffer
77 sufficiently large to hold the output string. This pointer should be passed to
78 \fBfree\fR(3C) to release the allocated storage when it is no longer needed. If
79 sufficient space cannot be allocated, the \fBasprintf()\fR function returns -1
80 and sets \fIret\fR to be a \fINULL\fR pointer.
81 .sp
82 .LP
83 Each of these functions converts, formats, and prints its arguments under
84 control of the \fIformat\fR. The \fIformat\fR is a character string, beginning
85 and ending in its initial shift state, if any. The \fIformat\fR is composed of
86 zero or more directives: \fBordinary characters\fR, which are simply copied to
87 the output stream and \fBconversion specifications\fR, each of which results in
88 the fetching of zero or more arguments. The results are undefined if there are
89 insufficient arguments for the \fIformat\fR. If the \fIformat\fR is exhausted
90 while arguments remain, the excess arguments are evaluated but are otherwise
91 ignored.
92 .sp
93 .LP
94 Conversions can be applied to the \fIn\fRth argument after the \fIformat\fR in
95 the argument list, rather than to the next unused argument. In this case, the
96 conversion specifier \fB%\fR (see below) is replaced by the sequence
97 \fB%\fR\fIn\fR\fB$\fR, where \fIn\fR is a decimal integer in the range [1,
98 \fBNL_ARGMAX\fR], giving the position of the argument in the argument list.
99 This feature provides for the definition of format strings that select
100 arguments in an order appropriate to specific languages (see the \fBEXAMPLES\fR
101 section).
104 In format strings containing the \fB%\fR\fIn\fR\fB$\fR form of conversion
105 specifications, numbered arguments in the argument list can be referenced from
106 the format string as many times as required.
109 In format strings containing the \fB%\fR form of conversion specifications,
110 each argument in the argument list is used exactly once.
113 All forms of the \fBprintf()\fR functions allow for the insertion of a
114 language-dependent radix character in the output string. The radix character is
115 defined by the program's locale (category \fBLC_NUMERIC\fR). In the POSIX
116 locale, or in a locale where the radix character is not defined, the radix
117 character defaults to a period (\fB\&.\fR).
118 .SS "Conversion Specifications"
121 Each conversion specification is introduced by the \fB%\fR character or by the
122 character sequence \fB%\fR\fIn\fR\fB$\fR, after which the following appear in
123 sequence:
124 .RS +4
126 .ie t \(bu
127 .el o
128 An optional field, consisting of a decimal digit string followed by a \fB$\fR,
129 specifying the next argument to be converted. If this field is not provided,
130 the \fIargs\fR following the last argument converted will be used.
132 .RS +4
134 .ie t \(bu
135 .el o
136 Zero or more \fIflags\fR (in any order), which modify the meaning of the
137 conversion specification.
139 .RS +4
141 .ie t \(bu
142 .el o
143 An optional minimum \fIfield width\fR. If the converted value has fewer bytes
144 than the field width, it will be padded with spaces by default on the left; it
145 will be padded on the right, if the left-adjustment flag (\fB\(hy\fR),
146 described below, is given to the field width. The field width takes the form of
147 an asterisk (*), described below, or a decimal integer.
149 If the conversion specifier is \fBs\fR, a standard-conforming application (see
150 \fBstandards\fR(5)) interprets the field width as the minimum number of bytes
151 to be printed; an application that is not standard-conforming interprets the
152 field width as the minimum number of columns of screen display. For an
153 application that is not standard-conforming, \fB%10s\fR means if the converted
154 value has a screen width of 7 columns, 3 spaces would be padded on the right.
156 If the format is \fB%ws\fR, then the field width should be interpreted as the
157 minimum number of columns of screen display.
159 .RS +4
161 .ie t \(bu
162 .el o
163 An optional \fIprecision\fR that gives the minimum number of digits to appear
164 for the \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, and \fBX\fR conversions
165 (the field is padded with leading zeros); the number of digits to appear after
166 the radix character for the \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, and
167 \fBF\fR conversions, the maximum number of significant digits for the \fBg\fR
168 and \fBG\fR conversions; or the maximum number of bytes to be printed from a
169 string in \fBs\fR and \fBS\fR conversions. The precision takes the form of a
170 period (.) followed either by an asterisk (*), described below, or an optional
171 decimal digit string, where a null digit string is treated as 0. If a precision
172 appears with any other conversion specifier, the behavior is undefined.
174 If the conversion specifier is \fBs\fR or \fBS\fR, a standard-conforming
175 application (see \fBstandards\fR(5)) interprets the precision as the maximum
176 number of bytes to be written; an application that is not standard-conforming
177 interprets the precision as the maximum number of columns of screen display.
178 For an application that is not standard-conforming, \fB%.5s\fR would print only
179 the portion of the string that would display in 5 screen columns. Only complete
180 characters are written.
182 For \fB%ws\fR, the precision should be interpreted as the maximum number of
183 columns of screen display. The precision takes the form of a period (\fB\&.\fR)
184 followed by a decimal digit string; a null digit string is treated as zero.
185 Padding specified by the precision overrides the padding specified by the field
186 width.
188 .RS +4
190 .ie t \(bu
191 .el o
192 An optional \fIlength modifier\fR that specified the size of the argument.
194 .RS +4
196 .ie t \(bu
197 .el o
198 A \fIconversion specifier\fR that indicates the type of conversion to be
199 applied.
203 A field width, or precision, or both can be indicated by an asterisk
204 (\fB*\fR) . In this case, an argument of type \fBint\fR supplies the field width or
205 precision. Arguments specifying field width, or precision, or both must appear
206 in that order before the argument, if any, to be converted. A negative field
207 width is taken as a \(mi flag followed by a positive field width. A negative
208 precision is taken as if the precision were omitted. In format strings
209 containing the \fB%\fR\fIn\fR\fB$\fR form of a conversion specification, a
210 field width or precision may be indicated by the sequence
211 \fB*\fR\fIm\fR\fB$\fR, where \fIm\fR is a decimal integer in the range [1,
212 \fBNL_ARGMAX\fR] giving the position in the argument list (after the format
213 argument) of an integer argument containing the field width or precision, for
214 example:
216 .in +2
218 printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec);
220 .in -2
224 The \fIformat\fR can contain either numbered argument specifications (that is,
225 \fB%\fR\fIn\fR\fB$\fR and \fB*\fR\fIm\fR\fB$\fR), or unnumbered argument
226 specifications (that is, \fB%\fR and \fB*\fR), but normally not both. The only
227 exception to this is that \fB%%\fR can be mixed with the \fB%\fR\fIn\fR\fB$\fR
228 form. The results of mixing numbered and unnumbered argument specifications in
229 a \fIformat\fR string are undefined. When numbered argument specifications are
230 used, specifying the \fIN\fRth argument requires that all the leading
231 arguments, from the first to the (\fIN-1\fR)th, are specified in the format
232 string.
233 .SS "Flag Characters"
236 The flag characters and their meanings are:
238 .ne 2
240 \fB\fB\&'\fR\fR
242 .RS 9n
243 The integer portion of the result of a decimal conversion (\fB%i\fR, \fB%d\fR,
244 \fB%u\fR, \fB%f\fR, \fB%F\fR, \fB%g\fR, or \fB%G\fR) will be formatted with
245 thousands' grouping characters. For other conversions the behavior is
246 undefined. The non-monetary grouping character is used.
250 .ne 2
252 \fB\fB\(mi\fR\fR
254 .RS 9n
255 The result of the conversion will be left-justified within the field. The
256 conversion will be right-justified if this flag is not specified.
260 .ne 2
262 \fB\fB+\fR\fR
264 .RS 9n
265 The result of a signed conversion will always begin with a sign (+ or -). The
266 conversion will begin with a sign only when a negative value is converted if
267 this flag is not specified.
271 .ne 2
273 \fB\fBspace\fR\fR
275 .RS 9n
276 If the first character of a signed conversion is not a sign or if a signed
277 conversion results in no characters, a space will be placed before the result.
278 This means that if the \fBspace\fR and \fB+\fR flags both appear, the space
279 flag will be ignored.
283 .ne 2
285 \fB\fB#\fR\fR
287 .RS 9n
288 The value is to be converted to an alternate form. For \fBc\fR, \fBd\fR,
289 \fBi\fR, \fBs\fR, and \fBu\fR conversions, the flag has no effect. For an
290 \fBo\fR conversion, it increases the precision (if necessary) to force the
291 first digit of the result to be a zero. For \fBx\fR or \fBX\fR conversion, a
292 non-zero result will have \fB0x\fR (or \fB0X\fR) prepended to it. For \fBa\fR,
293 \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions,
294 the result will always contain a radix character, even if no digits follow the
295 radix character. Without this flag, the radix character appears in the result
296 of these conversions only if a digit follows it. For \fBg\fR and \fBG\fR
297 conversions, trailing zeros will not be removed from the result as they
298 normally are.
302 .ne 2
304 \fB\fB0\fR\fR
306 .RS 9n
307 For \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, \fBX\fR, \fBa\fR, \fBA\fR,
308 \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions, leading
309 zeros (following any indication of sign or base) are used to pad to the field
310 width; no space padding is performed. If the \fB0\fR and \fB\(mi\fR flags both
311 appear, the \fB0\fR flag will be ignored. For \fBd\fR, \fBi\fR, \fBo\fR,
312 \fBu\fR, \fBx\fR, and \fBX\fR conversions, if a precision is specified, the
313 \fB0\fR flag will be ignored. If the \fB0\fR and \fB\&'\fR flags both appear,
314 the grouping characters are inserted before zero padding. For other
315 conversions, the behavior is undefined.
318 .SS "Length Modifiers"
321 The length modifiers and their meanings are:
323 .ne 2
325 \fB\fBhh\fR\fR
327 .RS 16n
328 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
329 \fBX\fR conversion specifier applies to a \fBsigned char\fR or \fBunsigned
330 char\fR argument (the argument will have been promoted according to the integer
331 promotions, but its value will be converted to \fBsigned char\fR or \fBunsigned
332 char\fR before printing); or that a following \fBn\fR conversion specifier
333 applies to a pointer to a \fBsigned char\fR argument.
337 .ne 2
339 \fB\fBh\fR\fR
341 .RS 16n
342 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
343 \fBX\fR conversion specifier applies to a \fBshort\fR or \fBunsigned short\fR
344 argument (the argument will have been promoted according to the integer
345 promotions, but its value will be converted to \fBshort\fR or \fBunsigned
346 short\fR before printing); or that a following \fBn\fR conversion specifier
347 applies to a pointer to a \fBshort\fR argument.
351 .ne 2
353 \fB\fBl (ell)\fR\fR
355 .RS 16n
356 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
357 \fBX\fR conversion specifier applies to a \fBlong\fR or \fBunsigned long\fR
358 argument; that a following \fBn\fR conversion specifier applies to a pointer to
359 a \fBlong\fR argument; that a following \fBc\fR conversion specifier applies to
360 a \fBwint_t\fR argument; that a following \fBs\fR conversion specifier applies
361 to a pointer to a \fBwchar_t\fR argument; or has no effect on a following
362 \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, or \fBG\fR
363 conversion specifier.
367 .ne 2
369 \fB\fBll (ell-ell)\fR\fR
371 .RS 16n
372 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
373 \fBX\fR conversion specifier applies to a \fBlong long\fR or \fBunsigned long
374 long\fR argument; or that a following \fBn\fR conversion specifier applies to a
375 pointer to a \fBlong long\fR argument.
379 .ne 2
381 \fB\fBj\fR\fR
383 .RS 16n
384 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
385 \fBX\fR conversion specifier applies to an \fBintmax_t\fR or \fBuintmax_t\fR
386 argument; or that a following \fBn\fR conversion specifier applies to a pointer
387 to an \fBintmax_t\fR argument. See NOTES.
391 .ne 2
393 \fB\fBz\fR\fR
395 .RS 16n
396 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
397 \fBX\fR conversion specifier applies to a \fBsize_t\fR or the corresponding
398 signed integer type argument; or that a following \fBn\fR conversion specifier
399 applies to a pointer to a signed integer type corresponding to \fBsize_t\fR
400 argument.
404 .ne 2
406 \fB\fBt\fR\fR
408 .RS 16n
409 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
410 \fBX\fR conversion specifier applies to a \fBptrdiff_t\fR or the corresponding
411 unsigned type argument; or that a following n conversion specifier applies to a
412 pointer to a \fBptrdiff_t\fR argument.
416 .ne 2
418 \fB\fBL\fR\fR
420 .RS 16n
421 Specifies that a following \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR,
422 \fBF\fR, \fBg\fR, or \fBG\fR conversion specifier applies to a \fBlong
423 double\fR argument.
428 If a length modifier appears with any conversion specifier other than as
429 specified above, the behavior is undefined.
430 .SS "Conversion Specifiers"
433 Each conversion specifier results in fetching zero or more arguments. The
434 results are undefined if there are insufficient arguments for the format. If
435 the format is exhausted while arguments remain, the excess arguments are
436 ignored.
439 The conversion specifiers and their meanings are:
441 .ne 2
443 \fB\fBd\fR, \fBi\fR\fR
445 .RS 8n
446 The \fBint\fR argument is converted to a signed decimal in the style
447 \fB[\fR\(mi\fB]\fR\fIdddd\fR. The precision specifies the minimum number of
448 digits to appear; if the value being converted can be represented in fewer
449 digits, it will be expanded with leading zeros. The default precision is 1. The
450 result of converting 0 with an explicit precision of 0 is no characters.
454 .ne 2
456 \fB\fBo\fR\fR
458 .RS 8n
459 The \fBunsigned int\fR argument is converted to unsigned octal format in the
460 style \fIdddd\fR. The precision specifies the minimum number of digits to
461 appear; if the value being converted can be represented in fewer digits, it
462 will be expanded with leading zeros. The default precision is 1. The result of
463 converting 0 with an explicit precision of 0 is no characters.
467 .ne 2
469 \fB\fBu\fR\fR
471 .RS 8n
472 The \fBunsigned int\fR argument is converted to unsigned decimal format in the
473 style \fIdddd\fR. The precision specifies the minimum number of digits to
474 appear; if the value being converted can be represented in fewer digits, it
475 will be expanded with leading zeros. The default precision is 1. The result of
476 converting 0 with an explicit precision of 0 is no characters.
480 .ne 2
482 \fB\fBx\fR\fR
484 .RS 8n
485 The \fBunsigned int\fR argument is converted to unsigned hexadecimal format in
486 the style \fIdddd\fR; the letters \fBabcdef\fR are used. The precision
487 specifies the minimum number of digits to appear; if the value being converted
488 can be represented in fewer digits, it will be expanded with leading zeros. The
489 default precision is 1. The result of converting 0 with an explicit precision
490 of 0 is no characters.
494 .ne 2
496 \fB\fBX\fR\fR
498 .RS 8n
499 Behaves the same as the \fBx\fR conversion specifier except that letters
500 \fBABCDEF\fR are used instead of \fBabcdef\fR.
504 .ne 2
506 \fB\fBf\fR, \fBF\fR\fR
508 .RS 8n
509 The \fBdouble\fR argument is converted to decimal notation in the style
510 [\fB\(mi\fR]\fIddd\fR\fB\&.\fR\fIddd\fR, where the number of digits after the
511 radix character (see \fBsetlocale\fR(3C)) is equal to the precision
512 specification. If the precision is missing it is taken as 6; if the precision
513 is explicitly 0 and the \fB#\fR flag is not specified, no radix character
514 appears. If a radix character appears, at least 1 digit appears before it. The
515 converted value is rounded to fit the specified output format according to the
516 prevailing floating point rounding direction mode. If the conversion is not
517 exact, an inexact exception is raised.
519 For the \fBf\fR specifier, a double argument representing an infinity or NaN is
520 converted in the style of the \fBe\fR conversion specifier, except that for an
521 infinite argument, "infinity" or "Infinity" is printed when the precision is at
522 least 8 and "inf" or "Inf" is printed otherwise.
524 For the F specifier, a double argument representing an infinity or NaN is
525 converted in the SUSv3 style of the E conversion specifier, except that for an
526 infinite argument, "INFINITY" is printed when the precision is at least 8 and
527 or "INF" is printed otherwise.
531 .ne 2
533 \fB\fBe\fR, \fBE\fR\fR
535 .RS 8n
536 The \fBdouble\fR argument is converted to the style
537 [\fB\(mi\fR]\fId\fR\fB\&.\fR\fIddd\fR\fBe\fR\fI\(+-dd\fR, where there is one
538 digit before the radix character (which is non-zero if the argument is
539 non-zero) and the number of digits after it is equal to the precision. When the
540 precision is missing it is taken as 6; if the precision is 0 and the \fB#\fR
541 flag is not specified, no radix character appears. The \fBE\fR conversion
542 specifier will produce a number with \fBE\fR instead of \fBe\fR introducing the
543 exponent. The exponent always contains at least two digits. The converted value
544 is rounded to fit the specified output format according to the prevailing
545 floating point rounding direction mode. If the conversion is not exact, an
546 inexact exception is raised.
548 Infinity and NaN values are handled in one of the following ways:
550 .ne 2
552 \fBSUSv3\fR
554 .RS 11n
555 For the \fBe\fR specifier, a \fBdouble\fR argument representing an infinity is
556 printed as "[\(mi]\fBinfinity\fR", when the precision for the conversion is at
557 least 7 and as "[\(mi]\fBinf\fR" otherwise. A \fBdouble\fR argument
558 representing a NaN is printed as "[\(mi]\fBnan\fR". For the \fBE\fR specifier,
559 "\fBINF\fR", "\fBINFINITY\fR", and "\fBNAN\fR" are printed instead of
560 "\fBinf\fR", "\fBinfinity\fR", and "\fBnan\fR", respectively. Printing of the
561 sign follows the rules described above.
565 .ne 2
567 \fBDefault\fR
569 .RS 11n
570 A \fBdouble\fR argument representing an infinity is printed as
571 "[\(mi]\fBInfinity\fR", when the precision for the conversion is at least 7 and
572 as "[\(mi]\fBInf\fR" otherwise. A double argument representing a NaN is printed
573 as "[\(mi]\fBNaN\fR". Printing of the sign follows the rules described above.
579 .ne 2
581 \fB\fBg\fR, \fBG\fR\fR
583 .RS 8n
584 The \fBdouble\fR argument is printed in style \fBf\fR or \fBe\fR (or in style
585 \fBE\fR in the case of a \fBG\fR conversion specifier), with the precision
586 specifying the number of significant digits. If an explicit precision is 0, it
587 is taken as 1. The style used depends on the value converted: style \fBe\fR (or
588 \fBE\fR) will be used only if the exponent resulting from the conversion is
589 less than -4 or greater than or equal to the precision. Trailing zeros are
590 removed from the fractional part of the result. A radix character appears only
591 if it is followed by a digit.
593 A \fBdouble\fR argument representing an infinity or NaN is converted in the
594 style of the \fBe\fR or \fBE\fR conversion specifier, except that for an
595 infinite argument, "infinity", "INFINITY", or "Infinity" is printed when the
596 precision is at least 8 and "inf", "INF", or "Inf" is printed otherwise.
600 .ne 2
602 \fB\fBa\fR, \fBA\fR\fR
604 .RS 8n
605 A \fBdouble\fR argument representing a floating-point number is converted in
606 the style "[-]0\fIxh\fR.\fIhhhhp\fR\(+-\fId\fR", where the single hexadecimal
607 digit preceding the radix point is 0 if the value converted is zero and 1
608 otherwise and the number of hexadecimal digits after it is equal to the
609 precision; if the precision is missing, the number of digits printed after the
610 radix point is 13 for the conversion of a double value, 16 for the conversion
611 of a long double value on x86, and 28 for the conversion of a long double value
612 on SPARC; if the precision is zero and the '#' flag is not specified, no
613 decimal-point character will appear. The letters "\fBabcdef\fR" are used for
614 \fBa\fR conversion and the letters "\fBABCDEF\fR" for \fBA\fR conversion. The
615 \fBA\fR conversion specifier produces a number with '\fBX\fR' and '\fBP\fR'
616 instead of '\fBx\fR' and '\fBp\fR'. The exponent will always contain at least
617 one digit, and only as many more digits as necessary to represent the decimal
618 exponent of 2. If the value is zero, the exponent is zero.
620 The converted value is rounded to fit the specified output format according to
621 the prevailing floating point rounding direction mode. If the conversion is not
622 exact, an inexact exception is raised.
624 A \fBdouble\fR argument representing an infinity or NaN is converted in the
625 SUSv3 style of an \fBe\fR or \fBE\fR conversion specifier.
629 .ne 2
631 \fB\fBc\fR\fR
633 .RS 8n
634 The \fBint\fR argument is converted to an \fBunsigned char\fR, and the
635 resulting byte is printed.
637 If an \fBl\fR (ell) qualifier is present, the \fBwint_t\fR argument is
638 converted as if by an \fBls\fR conversion specification with no precision and
639 an argument that points to a two-element array of type \fBwchar_t\fR, the first
640 element of which contains the \fBwint_t\fR argument to the \fBls\fR conversion
641 specification and the second element contains a null wide-character.
645 .ne 2
647 \fB\fBC\fR\fR
649 .RS 8n
650 Same as \fBlc\fR.
654 .ne 2
656 \fB\fBwc\fR\fR
658 .RS 8n
659 The \fBint\fR argument is converted to a wide character (\fBwchar_t\fR), and
660 the resulting wide character is printed.
664 .ne 2
666 \fB\fBs\fR\fR
668 .RS 8n
669 The argument must be a pointer to an array of \fBchar\fR. Bytes from the array
670 are written up to (but not including) any terminating null byte. If a precision
671 is specified, a standard-conforming application (see \fBstandards\fR(5)) will
672 write only the number of bytes specified by precision; an application that is
673 not standard-conforming will write only the portion of the string that will
674 display in the number of columns of screen display specified by precision. If
675 the precision is not specified, it is taken to be infinite, so all bytes up to
676 the first null byte are printed. An argument with a null value will yield
677 undefined results.
679 If an \fBl\fR (ell) qualifier is present, the argument must be a pointer to an
680 array of type \fBwchar_t\fR. Wide-characters from the array are converted to
681 characters (each as if by a call to the \fBwcrtomb\fR(3C) function, with the
682 conversion state described by an \fBmbstate_t\fR object initialized to zero
683 before the first wide-character is converted) up to and including a terminating
684 null wide-character. The resulting characters are written up to (but not
685 including) the terminating null character (byte). If no precision is specified,
686 the array must contain a null wide-character. If a precision is specified, no
687 more than that many characters (bytes) are written (including shift sequences,
688 if any), and the array must contain a null wide-character if, to equal the
689 character sequence length given by the precision, the function would need to
690 access a wide-character one past the end of the array. In no case is a partial
691 character written.
695 .ne 2
697 \fB\fBS\fR\fR
699 .RS 8n
700 Same as \fBls\fR.
704 .ne 2
706 \fB\fBws\fR\fR
708 .RS 8n
709 The argument must be a pointer to an array of \fBwchar_t\fR. Bytes from the
710 array are written up to (but not including) any terminating null character. If
711 the precision is specified, only that portion of the wide-character array that
712 will display in the number of columns of screen display specified by precision
713 will be written. If the precision is not specified, it is taken to be infinite,
714 so all wide characters up to the first null character are printed. An argument
715 with a null value will yield undefined results.
719 .ne 2
721 \fB\fBp\fR\fR
723 .RS 8n
724 The argument must be a pointer to \fBvoid\fR. The value of the pointer is
725 converted to a set of sequences of printable characters, which should be the
726 same as the set of sequences that are matched by the \fB%p\fR conversion of the
727 \fBscanf\fR(3C) function.
731 .ne 2
733 \fB\fBn\fR\fR
735 .RS 8n
736 The argument must be a pointer to an integer into which is written the number
737 of bytes written to the output standard I/O stream so far by this call to one
738 of the \fBprintf()\fR functions. No argument is converted.
742 .ne 2
744 \fB\fB%\fR\fR
746 .RS 8n
747 Print a \fB%\fR; no argument is converted. The entire conversion specification
748 must be %%.
753 If a conversion specification does not match one of the above forms, the
754 behavior is undefined.
757 In no case does a non-existent or small field width cause truncation of a
758 field; if the result of a conversion is wider than the field width, the field
759 is simply expanded to contain the conversion result. Characters generated by
760 \fBprintf()\fR and \fBfprintf()\fR are printed as if the \fBputc\fR(3C)
761 function had been called.
764 The \fBst_ctime\fR and \fBst_mtime\fR fields of the file will be marked for
765 update between the call to a successful execution of \fBprintf()\fR or
766 \fBfprintf()\fR and the next successful completion of a call to
767 \fBfflush\fR(3C) or \fBfclose\fR(3C) on the same stream or a call to
768 \fBexit\fR(3C) or \fBabort\fR(3C).
769 .SH RETURN VALUES
772 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBasprintf()\fR
773 functions return the number of bytes transmitted (excluding the terminating
774 null byte in the case of \fBsprintf()\fR and \fBasprintf()\fR).
777 The \fBsnprintf()\fR function returns the number of bytes that would have been
778 written to \fIs\fR if \fIn\fR had been sufficiently large (excluding the
779 terminating null byte.) If the value of \fIn\fR is 0 on a call to
780 \fBsnprintf()\fR, \fIs\fR can be a null pointer and the number of bytes that
781 would have been written if \fIn\fR had been sufficiently large (excluding the
782 terminating null byte) is returned.
785 Each function returns a negative value if an output error was encountered.
786 .SH ERRORS
789 For the conditions under which \fBprintf()\fR and \fBfprintf()\fR will fail and
790 may fail, refer to \fBfputc\fR(3C) or \fBfputwc\fR(3C).
793 The \fBsnprintf()\fR function will fail if:
795 .ne 2
797 \fB\fBEOVERFLOW\fR\fR
799 .RS 13n
800 The value of \fIn\fR is greater than \fBINT_MAX\fR or the number of bytes
801 needed to hold the output excluding the terminating null is greater than
802 \fBINT_MAX\fR.
807 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR
808 functions may fail if:
810 .ne 2
812 \fB\fBEILSEQ\fR\fR
814 .RS 10n
815 A wide-character code that does not correspond to a valid character has been
816 detected.
820 .ne 2
822 \fB\fBEINVAL\fR\fR
824 .RS 10n
825 There are insufficient arguments.
830 The \fBprintf()\fR, \fBfprintf()\fR, and \fBasprintf()\fR functions may fail
831 due to an underlying \fBmalloc\fR(3C) failure if:
833 .ne 2
835 \fB\fBEAGAIN\fR\fR
837 .RS 10n
838 Storage space is temporarily unavailable.
842 .ne 2
844 \fB\fBENOMEM\fR\fR
846 .RS 10n
847 Insufficient storage space is available.
850 .SH USAGE
853 If the application calling the \fBprintf()\fR functions has any objects of type
854 \fBwint_t\fR or \fBwchar_t\fR, it must also include the header \fB<wchar.h>\fR
855 to have these objects defined.
856 .SS "Escape Character Sequences"
859 It is common to use the following escape sequences built into the C language
860 when entering format strings for the \fBprintf()\fR functions, but these
861 sequences are processed by the C compiler, not by the \fBprintf()\fR function.
863 .ne 2
865 \fB\fB\ea\fR\fR
867 .RS 7n
868 Alert. Ring the bell.
872 .ne 2
874 \fB\fB\eb\fR\fR
876 .RS 7n
877 Backspace. Move the printing position to one character before the current
878 position, unless the current position is the start of a line.
882 .ne 2
884 \fB\fB\ef\fR\fR
886 .RS 7n
887 Form feed. Move the printing position to the initial printing position of the
888 next logical page.
892 .ne 2
894 \fB\fB\en\fR\fR
896 .RS 7n
897 Newline. Move the printing position to the start of the next line.
901 .ne 2
903 \fB\fB\er\fR\fR
905 .RS 7n
906 Carriage return. Move the printing position to the start of the current line.
910 .ne 2
912 \fB\fB\et\fR\fR
914 .RS 7n
915 Horizontal tab. Move the printing position to the next implementation-defined
916 horizontal tab position on the current line.
920 .ne 2
922 \fB\fB\ev\fR\fR
924 .RS 7n
925 Vertical tab. Move the printing position to the start of the next
926 implementation-defined vertical tab position.
931 In addition, the C language supports character sequences of the form
934 \eoctal-number
940 \ehex-number
943 which translates into the character represented by the octal or hexadecimal
944 number. For example, if ASCII representations are being used, the letter 'a'
945 may be written as '\e141' and 'Z' as '\e132'. This syntax is most frequently
946 used to represent the null character as '\e0'. This is exactly equivalent to
947 the numeric constant zero (0). Note that the octal number does not include the
948 zero prefix as it would for a normal octal constant. To specify a hexadecimal
949 number, omit the zero so that the prefix is an 'x' (uppercase 'X' is not
950 allowed in this context). Support for hexadecimal sequences is an ANSI
951 extension. See \fBstandards\fR(5).
952 .SH EXAMPLES
954 \fBExample 1 \fRTo print the language-independent date and time format, the
955 following statement could be used:
957 .in +2
959 \fBprintf (format, weekday, month, day, hour, min);\fR
961 .in -2
965 For American usage, \fIformat\fR could be a pointer to the string:
968 .in +2
970 \fB"%s, %s %d, %d:%.2d\en"\fR
972 .in -2
976 producing the message:
979 .in +2
981 \fBSunday, July 3, 10:02\fR
983 .in -2
987 whereas for German usage, \fIformat\fR could be a pointer to the string:
990 .in +2
992 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
994 .in -2
998 producing the message:
1001 .in +2
1003 Sonntag, 3. Juli, 10:02
1005 .in -2
1008 \fBExample 2 \fRTo print a date and time in the form \fBSunday, July 3,
1009 10:02\fR, where \fBweekday\fR and \fBmonth\fR are pointers to null-terminated
1010 strings:
1012 .in +2
1014 printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
1016 .in -2
1019 \fBExample 3 \fRTo print pi to 5 decimal places:
1021 .in +2
1023 printf("pi = %.5f", 4 * atan(1.0));
1025 .in -2
1027 .SS "Default"
1029 \fBExample 4 \fRThe following example applies only to applications that are not
1030 standard-conforming. To print a list of names in columns which are 20
1031 characters wide:
1033 .in +2
1035 \fBprintf("%20s%20s%20s", lastname, firstname, middlename);\fR
1037 .in -2
1039 .SH ATTRIBUTES
1042 See \fBattributes\fR(5) for descriptions of the following attributes:
1047 box;
1048 l | l
1049 l | l .
1050 ATTRIBUTE TYPE  ATTRIBUTE VALUE
1052 CSI     Enabled
1054 Interface Stability     Committed
1056 MT-Level        See below.
1058 Standard        See below.
1063 All of these functions can be used safely in multithreaded applications, as
1064 long as \fBsetlocale\fR(3C) is not being called to change the locale. The
1065 \fBsprintf()\fR and \fBsnprintf()\fR functions are Async-Signal-Safe.
1068 See \fBstandards\fR(5) for the standards conformance of \fBprintf()\fR,
1069 \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR. The \fBasprintf()\fR
1070 function is modeled on the one that appears in the FreeBSD, NetBSD, and GNU C
1071 libraries.
1072 .SH SEE ALSO
1075 \fBexit\fR(2), \fBlseek\fR(2), \fBwrite\fR(2), \fBabort\fR(3C), \fBecvt\fR(3C),
1076 \fBexit\fR(3C), \fBfclose\fR(3C), \fBfflush\fR(3C), \fBfputwc\fR(3C),
1077 \fBfree\fR(3C), \fBmalloc\fR(3C), \fBputc\fR(3C), \fBscanf\fR(3C),
1078 \fBsetlocale\fR(3C), \fBstdio\fR(3C), \fBvprintf\fR(3C), \fBwcstombs\fR(3C),
1079 \fBwctomb\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), \fBstandards\fR(5)
1080 .SH NOTES
1083 If the \fBj\fR length modifier is used, 32-bit applications that were compiled
1084 using \fBc89\fR on releases prior to Solaris 10 will experience undefined
1085 behavior.
1088 The \fBsnprintf()\fR return value when \fIn\fR = 0 was changed in the Solaris
1089 10 release. The change was based on the SUSv3 specification. The previous
1090 behavior was based on the initial SUSv2 specification, where \fBsnprintf()\fR
1091 when \fIn\fR = 0 returns an unspecified value less than 1.