2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved.
45 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved.
46 .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved.
48 .TH PRINTF 3C "Jan 7, 2009"
50 printf, fprintf, sprintf, snprintf, asprintf \- print formatted output
56 \fBint\fR \fBprintf\fR(\fBconst char *restrict\fR \fIformat\fR,
57 \fB/*\fR \fIargs\fR*/ ...);
62 \fBint\fR \fBfprintf\fR(\fBFILE *restrict\fR \fIstream\fR, \fBconst char *restrict\fR \fIformat\fR,
63 \fB/*\fR \fIargs\fR*/ ...);
68 \fBint\fR \fBsprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBconst char *restrict\fR \fIformat\fR,
69 \fB/*\fR \fIargs\fR*/ ...);
74 \fBint\fR \fBsnprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBsize_t\fR \fIn\fR,
75 \fBconst char *restrict\fR \fIformat\fR, \fB/*\fR \fIargs\fR*/ ...);
80 \fBint\fR \fBasprintf\fR(\fBchar **\fR \fIret\fR, \fBconst char *restrict\fR \fIformat\fR,
81 \fB/*\fR \fIargs\fR*/ ...);
87 The \fBprintf()\fR function places output on the standard output stream
91 The \fBfprintf()\fR function places output on on the named output stream
95 The \fBsprintf()\fR function places output, followed by the null byte
96 (\fB\e0\fR), in consecutive bytes starting at \fIs\fR; it is the user's
97 responsibility to ensure that enough storage is available.
100 The \fBsnprintf()\fR function is identical to \fBsprintf()\fR with the addition
101 of the argument \fIn\fR, which specifies the size of the buffer referred to by
102 \fIs\fR. If \fIn\fR is 0, nothing is written and \fIs\fR can be a null pointer.
103 Otherwise, output bytes beyond the \fIn\fR-1st are discarded instead of being
104 written to the array and a null byte is written at the end of the bytes
105 actually written into the array.
108 The \fBasprintf()\fR function is the same as the \fBsprintf()\fR function
109 except that it returns, in the \fIret\fR argument, a pointer to a buffer
110 sufficiently large to hold the output string. This pointer should be passed to
111 \fBfree\fR(3C) to release the allocated storage when it is no longer needed. If
112 sufficient space cannot be allocated, the \fBasprintf()\fR function returns -1
113 and sets \fIret\fR to be a \fINULL\fR pointer.
116 Each of these functions converts, formats, and prints its arguments under
117 control of the \fIformat\fR. The \fIformat\fR is a character string, beginning
118 and ending in its initial shift state, if any. The \fIformat\fR is composed of
119 zero or more directives: \fBordinary characters\fR, which are simply copied to
120 the output stream and \fBconversion specifications\fR, each of which results in
121 the fetching of zero or more arguments. The results are undefined if there are
122 insufficient arguments for the \fIformat\fR. If the \fIformat\fR is exhausted
123 while arguments remain, the excess arguments are evaluated but are otherwise
127 Conversions can be applied to the \fIn\fRth argument after the \fIformat\fR in
128 the argument list, rather than to the next unused argument. In this case, the
129 conversion specifier \fB%\fR (see below) is replaced by the sequence
130 \fB%\fR\fIn\fR\fB$\fR, where \fIn\fR is a decimal integer in the range [1,
131 \fBNL_ARGMAX\fR], giving the position of the argument in the argument list.
132 This feature provides for the definition of format strings that select
133 arguments in an order appropriate to specific languages (see the \fBEXAMPLES\fR
137 In format strings containing the \fB%\fR\fIn\fR\fB$\fR form of conversion
138 specifications, numbered arguments in the argument list can be referenced from
139 the format string as many times as required.
142 In format strings containing the \fB%\fR form of conversion specifications,
143 each argument in the argument list is used exactly once.
146 All forms of the \fBprintf()\fR functions allow for the insertion of a
147 language-dependent radix character in the output string. The radix character is
148 defined by the program's locale (category \fBLC_NUMERIC\fR). In the POSIX
149 locale, or in a locale where the radix character is not defined, the radix
150 character defaults to a period (\fB\&.\fR).
151 .SS "Conversion Specifications"
154 Each conversion specification is introduced by the \fB%\fR character or by the
155 character sequence \fB%\fR\fIn\fR\fB$\fR, after which the following appear in
161 An optional field, consisting of a decimal digit string followed by a \fB$\fR,
162 specifying the next argument to be converted. If this field is not provided,
163 the \fIargs\fR following the last argument converted will be used.
169 Zero or more \fIflags\fR (in any order), which modify the meaning of the
170 conversion specification.
176 An optional minimum \fIfield width\fR. If the converted value has fewer bytes
177 than the field width, it will be padded with spaces by default on the left; it
178 will be padded on the right, if the left-adjustment flag (\fB\(hy\fR),
179 described below, is given to the field width. The field width takes the form of
180 an asterisk (*), described below, or a decimal integer.
182 If the conversion specifier is \fBs\fR, a standard-conforming application (see
183 \fBstandards\fR(5)) interprets the field width as the minimum number of bytes
184 to be printed; an application that is not standard-conforming interprets the
185 field width as the minimum number of columns of screen display. For an
186 application that is not standard-conforming, \fB%10s\fR means if the converted
187 value has a screen width of 7 columns, 3 spaces would be padded on the right.
189 If the format is \fB%ws\fR, then the field width should be interpreted as the
190 minimum number of columns of screen display.
196 An optional \fIprecision\fR that gives the minimum number of digits to appear
197 for the \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, and \fBX\fR conversions
198 (the field is padded with leading zeros); the number of digits to appear after
199 the radix character for the \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, and
200 \fBF\fR conversions, the maximum number of significant digits for the \fBg\fR
201 and \fBG\fR conversions; or the maximum number of bytes to be printed from a
202 string in \fBs\fR and \fBS\fR conversions. The precision takes the form of a
203 period (.) followed either by an asterisk (*), described below, or an optional
204 decimal digit string, where a null digit string is treated as 0. If a precision
205 appears with any other conversion specifier, the behavior is undefined.
207 If the conversion specifier is \fBs\fR or \fBS\fR, a standard-conforming
208 application (see \fBstandards\fR(5)) interprets the precision as the maximum
209 number of bytes to be written; an application that is not standard-conforming
210 interprets the precision as the maximum number of columns of screen display.
211 For an application that is not standard-conforming, \fB%.5s\fR would print only
212 the portion of the string that would display in 5 screen columns. Only complete
213 characters are written.
215 For \fB%ws\fR, the precision should be interpreted as the maximum number of
216 columns of screen display. The precision takes the form of a period (\fB\&.\fR)
217 followed by a decimal digit string; a null digit string is treated as zero.
218 Padding specified by the precision overrides the padding specified by the field
225 An optional \fIlength modifier\fR that specified the size of the argument.
231 A \fIconversion specifier\fR that indicates the type of conversion to be
236 A field width, or precision, or both can be indicated by an asterisk
237 (\fB*\fR) . In this case, an argument of type \fBint\fR supplies the field width or
238 precision. Arguments specifying field width, or precision, or both must appear
239 in that order before the argument, if any, to be converted. A negative field
240 width is taken as a \(mi flag followed by a positive field width. A negative
241 precision is taken as if the precision were omitted. In format strings
242 containing the \fB%\fR\fIn\fR\fB$\fR form of a conversion specification, a
243 field width or precision may be indicated by the sequence
244 \fB*\fR\fIm\fR\fB$\fR, where \fIm\fR is a decimal integer in the range [1,
245 \fBNL_ARGMAX\fR] giving the position in the argument list (after the format
246 argument) of an integer argument containing the field width or precision, for
251 printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec);
257 The \fIformat\fR can contain either numbered argument specifications (that is,
258 \fB%\fR\fIn\fR\fB$\fR and \fB*\fR\fIm\fR\fB$\fR), or unnumbered argument
259 specifications (that is, \fB%\fR and \fB*\fR), but normally not both. The only
260 exception to this is that \fB%%\fR can be mixed with the \fB%\fR\fIn\fR\fB$\fR
261 form. The results of mixing numbered and unnumbered argument specifications in
262 a \fIformat\fR string are undefined. When numbered argument specifications are
263 used, specifying the \fIN\fRth argument requires that all the leading
264 arguments, from the first to the (\fIN-1\fR)th, are specified in the format
266 .SS "Flag Characters"
269 The flag characters and their meanings are:
276 The integer portion of the result of a decimal conversion (\fB%i\fR, \fB%d\fR,
277 \fB%u\fR, \fB%f\fR, \fB%F\fR, \fB%g\fR, or \fB%G\fR) will be formatted with
278 thousands' grouping characters. For other conversions the behavior is
279 undefined. The non-monetary grouping character is used.
288 The result of the conversion will be left-justified within the field. The
289 conversion will be right-justified if this flag is not specified.
298 The result of a signed conversion will always begin with a sign (+ or -). The
299 conversion will begin with a sign only when a negative value is converted if
300 this flag is not specified.
309 If the first character of a signed conversion is not a sign or if a signed
310 conversion results in no characters, a space will be placed before the result.
311 This means that if the \fBspace\fR and \fB+\fR flags both appear, the space
312 flag will be ignored.
321 The value is to be converted to an alternate form. For \fBc\fR, \fBd\fR,
322 \fBi\fR, \fBs\fR, and \fBu\fR conversions, the flag has no effect. For an
323 \fBo\fR conversion, it increases the precision (if necessary) to force the
324 first digit of the result to be a zero. For \fBx\fR or \fBX\fR conversion, a
325 non-zero result will have \fB0x\fR (or \fB0X\fR) prepended to it. For \fBa\fR,
326 \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions,
327 the result will always contain a radix character, even if no digits follow the
328 radix character. Without this flag, the radix character appears in the result
329 of these conversions only if a digit follows it. For \fBg\fR and \fBG\fR
330 conversions, trailing zeros will not be removed from the result as they
340 For \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, \fBX\fR, \fBa\fR, \fBA\fR,
341 \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions, leading
342 zeros (following any indication of sign or base) are used to pad to the field
343 width; no space padding is performed. If the \fB0\fR and \fB\(mi\fR flags both
344 appear, the \fB0\fR flag will be ignored. For \fBd\fR, \fBi\fR, \fBo\fR,
345 \fBu\fR, \fBx\fR, and \fBX\fR conversions, if a precision is specified, the
346 \fB0\fR flag will be ignored. If the \fB0\fR and \fB\&'\fR flags both appear,
347 the grouping characters are inserted before zero padding. For other
348 conversions, the behavior is undefined.
351 .SS "Length Modifiers"
354 The length modifiers and their meanings are:
361 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
362 \fBX\fR conversion specifier applies to a \fBsigned char\fR or \fBunsigned
363 char\fR argument (the argument will have been promoted according to the integer
364 promotions, but its value will be converted to \fBsigned char\fR or \fBunsigned
365 char\fR before printing); or that a following \fBn\fR conversion specifier
366 applies to a pointer to a \fBsigned char\fR argument.
375 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
376 \fBX\fR conversion specifier applies to a \fBshort\fR or \fBunsigned short\fR
377 argument (the argument will have been promoted according to the integer
378 promotions, but its value will be converted to \fBshort\fR or \fBunsigned
379 short\fR before printing); or that a following \fBn\fR conversion specifier
380 applies to a pointer to a \fBshort\fR argument.
389 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
390 \fBX\fR conversion specifier applies to a \fBlong\fR or \fBunsigned long\fR
391 argument; that a following \fBn\fR conversion specifier applies to a pointer to
392 a \fBlong\fR argument; that a following \fBc\fR conversion specifier applies to
393 a \fBwint_t\fR argument; that a following \fBs\fR conversion specifier applies
394 to a pointer to a \fBwchar_t\fR argument; or has no effect on a following
395 \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, or \fBG\fR
396 conversion specifier.
402 \fB\fBll (ell-ell)\fR\fR
405 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
406 \fBX\fR conversion specifier applies to a \fBlong long\fR or \fBunsigned long
407 long\fR argument; or that a following \fBn\fR conversion specifier applies to a
408 pointer to a \fBlong long\fR argument.
417 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
418 \fBX\fR conversion specifier applies to an \fBintmax_t\fR or \fBuintmax_t\fR
419 argument; or that a following \fBn\fR conversion specifier applies to a pointer
420 to an \fBintmax_t\fR argument. See NOTES.
429 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
430 \fBX\fR conversion specifier applies to a \fBsize_t\fR or the corresponding
431 signed integer type argument; or that a following \fBn\fR conversion specifier
432 applies to a pointer to a signed integer type corresponding to \fBsize_t\fR
442 Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or
443 \fBX\fR conversion specifier applies to a \fBptrdiff_t\fR or the corresponding
444 unsigned type argument; or that a following n conversion specifier applies to a
445 pointer to a \fBptrdiff_t\fR argument.
454 Specifies that a following \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR,
455 \fBF\fR, \fBg\fR, or \fBG\fR conversion specifier applies to a \fBlong
461 If a length modifier appears with any conversion specifier other than as
462 specified above, the behavior is undefined.
463 .SS "Conversion Specifiers"
466 Each conversion specifier results in fetching zero or more arguments. The
467 results are undefined if there are insufficient arguments for the format. If
468 the format is exhausted while arguments remain, the excess arguments are
472 The conversion specifiers and their meanings are:
476 \fB\fBd\fR, \fBi\fR\fR
479 The \fBint\fR argument is converted to a signed decimal in the style
480 \fB[\fR\(mi\fB]\fR\fIdddd\fR. The precision specifies the minimum number of
481 digits to appear; if the value being converted can be represented in fewer
482 digits, it will be expanded with leading zeros. The default precision is 1. The
483 result of converting 0 with an explicit precision of 0 is no characters.
492 The \fBunsigned int\fR argument is converted to unsigned octal format in the
493 style \fIdddd\fR. The precision specifies the minimum number of digits to
494 appear; if the value being converted can be represented in fewer digits, it
495 will be expanded with leading zeros. The default precision is 1. The result of
496 converting 0 with an explicit precision of 0 is no characters.
505 The \fBunsigned int\fR argument is converted to unsigned decimal format in the
506 style \fIdddd\fR. The precision specifies the minimum number of digits to
507 appear; if the value being converted can be represented in fewer digits, it
508 will be expanded with leading zeros. The default precision is 1. The result of
509 converting 0 with an explicit precision of 0 is no characters.
518 The \fBunsigned int\fR argument is converted to unsigned hexadecimal format in
519 the style \fIdddd\fR; the letters \fBabcdef\fR are used. The precision
520 specifies the minimum number of digits to appear; if the value being converted
521 can be represented in fewer digits, it will be expanded with leading zeros. The
522 default precision is 1. The result of converting 0 with an explicit precision
523 of 0 is no characters.
532 Behaves the same as the \fBx\fR conversion specifier except that letters
533 \fBABCDEF\fR are used instead of \fBabcdef\fR.
539 \fB\fBf\fR, \fBF\fR\fR
542 The \fBdouble\fR argument is converted to decimal notation in the style
543 [\fB\(mi\fR]\fIddd\fR\fB\&.\fR\fIddd\fR, where the number of digits after the
544 radix character (see \fBsetlocale\fR(3C)) is equal to the precision
545 specification. If the precision is missing it is taken as 6; if the precision
546 is explicitly 0 and the \fB#\fR flag is not specified, no radix character
547 appears. If a radix character appears, at least 1 digit appears before it. The
548 converted value is rounded to fit the specified output format according to the
549 prevailing floating point rounding direction mode. If the conversion is not
550 exact, an inexact exception is raised.
552 For the \fBf\fR specifier, a double argument representing an infinity or NaN is
553 converted in the style of the \fBe\fR conversion specifier, except that for an
554 infinite argument, "infinity" or "Infinity" is printed when the precision is at
555 least 8 and "inf" or "Inf" is printed otherwise.
557 For the F specifier, a double argument representing an infinity or NaN is
558 converted in the SUSv3 style of the E conversion specifier, except that for an
559 infinite argument, "INFINITY" is printed when the precision is at least 8 and
560 or "INF" is printed otherwise.
566 \fB\fBe\fR, \fBE\fR\fR
569 The \fBdouble\fR argument is converted to the style
570 [\fB\(mi\fR]\fId\fR\fB\&.\fR\fIddd\fR\fBe\fR\fI\(+-dd\fR, where there is one
571 digit before the radix character (which is non-zero if the argument is
572 non-zero) and the number of digits after it is equal to the precision. When the
573 precision is missing it is taken as 6; if the precision is 0 and the \fB#\fR
574 flag is not specified, no radix character appears. The \fBE\fR conversion
575 specifier will produce a number with \fBE\fR instead of \fBe\fR introducing the
576 exponent. The exponent always contains at least two digits. The converted value
577 is rounded to fit the specified output format according to the prevailing
578 floating point rounding direction mode. If the conversion is not exact, an
579 inexact exception is raised.
581 Infinity and NaN values are handled in one of the following ways:
588 For the \fBe\fR specifier, a \fBdouble\fR argument representing an infinity is
589 printed as "[\(mi]\fBinfinity\fR", when the precision for the conversion is at
590 least 7 and as "[\(mi]\fBinf\fR" otherwise. A \fBdouble\fR argument
591 representing a NaN is printed as "[\(mi]\fBnan\fR". For the \fBE\fR specifier,
592 "\fBINF\fR", "\fBINFINITY\fR", and "\fBNAN\fR" are printed instead of
593 "\fBinf\fR", "\fBinfinity\fR", and "\fBnan\fR", respectively. Printing of the
594 sign follows the rules described above.
603 A \fBdouble\fR argument representing an infinity is printed as
604 "[\(mi]\fBInfinity\fR", when the precision for the conversion is at least 7 and
605 as "[\(mi]\fBInf\fR" otherwise. A double argument representing a NaN is printed
606 as "[\(mi]\fBNaN\fR". Printing of the sign follows the rules described above.
614 \fB\fBg\fR, \fBG\fR\fR
617 The \fBdouble\fR argument is printed in style \fBf\fR or \fBe\fR (or in style
618 \fBE\fR in the case of a \fBG\fR conversion specifier), with the precision
619 specifying the number of significant digits. If an explicit precision is 0, it
620 is taken as 1. The style used depends on the value converted: style \fBe\fR (or
621 \fBE\fR) will be used only if the exponent resulting from the conversion is
622 less than -4 or greater than or equal to the precision. Trailing zeros are
623 removed from the fractional part of the result. A radix character appears only
624 if it is followed by a digit.
626 A \fBdouble\fR argument representing an infinity or NaN is converted in the
627 style of the \fBe\fR or \fBE\fR conversion specifier, except that for an
628 infinite argument, "infinity", "INFINITY", or "Infinity" is printed when the
629 precision is at least 8 and "inf", "INF", or "Inf" is printed otherwise.
635 \fB\fBa\fR, \fBA\fR\fR
638 A \fBdouble\fR argument representing a floating-point number is converted in
639 the style "[-]0\fIxh\fR.\fIhhhhp\fR\(+-\fId\fR", where the single hexadecimal
640 digit preceding the radix point is 0 if the value converted is zero and 1
641 otherwise and the number of hexadecimal digits after it is equal to the
642 precision; if the precision is missing, the number of digits printed after the
643 radix point is 13 for the conversion of a double value, 16 for the conversion
644 of a long double value on x86, and 28 for the conversion of a long double value
645 on SPARC; if the precision is zero and the '#' flag is not specified, no
646 decimal-point character will appear. The letters "\fBabcdef\fR" are used for
647 \fBa\fR conversion and the letters "\fBABCDEF\fR" for \fBA\fR conversion. The
648 \fBA\fR conversion specifier produces a number with '\fBX\fR' and '\fBP\fR'
649 instead of '\fBx\fR' and '\fBp\fR'. The exponent will always contain at least
650 one digit, and only as many more digits as necessary to represent the decimal
651 exponent of 2. If the value is zero, the exponent is zero.
653 The converted value is rounded to fit the specified output format according to
654 the prevailing floating point rounding direction mode. If the conversion is not
655 exact, an inexact exception is raised.
657 A \fBdouble\fR argument representing an infinity or NaN is converted in the
658 SUSv3 style of an \fBe\fR or \fBE\fR conversion specifier.
667 The \fBint\fR argument is converted to an \fBunsigned char\fR, and the
668 resulting byte is printed.
670 If an \fBl\fR (ell) qualifier is present, the \fBwint_t\fR argument is
671 converted as if by an \fBls\fR conversion specification with no precision and
672 an argument that points to a two-element array of type \fBwchar_t\fR, the first
673 element of which contains the \fBwint_t\fR argument to the \fBls\fR conversion
674 specification and the second element contains a null wide-character.
692 The \fBint\fR argument is converted to a wide character (\fBwchar_t\fR), and
693 the resulting wide character is printed.
702 The argument must be a pointer to an array of \fBchar\fR. Bytes from the array
703 are written up to (but not including) any terminating null byte. If a precision
704 is specified, a standard-conforming application (see \fBstandards\fR(5)) will
705 write only the number of bytes specified by precision; an application that is
706 not standard-conforming will write only the portion of the string that will
707 display in the number of columns of screen display specified by precision. If
708 the precision is not specified, it is taken to be infinite, so all bytes up to
709 the first null byte are printed. An argument with a null value will yield
712 If an \fBl\fR (ell) qualifier is present, the argument must be a pointer to an
713 array of type \fBwchar_t\fR. Wide-characters from the array are converted to
714 characters (each as if by a call to the \fBwcrtomb\fR(3C) function, with the
715 conversion state described by an \fBmbstate_t\fR object initialized to zero
716 before the first wide-character is converted) up to and including a terminating
717 null wide-character. The resulting characters are written up to (but not
718 including) the terminating null character (byte). If no precision is specified,
719 the array must contain a null wide-character. If a precision is specified, no
720 more than that many characters (bytes) are written (including shift sequences,
721 if any), and the array must contain a null wide-character if, to equal the
722 character sequence length given by the precision, the function would need to
723 access a wide-character one past the end of the array. In no case is a partial
742 The argument must be a pointer to an array of \fBwchar_t\fR. Bytes from the
743 array are written up to (but not including) any terminating null character. If
744 the precision is specified, only that portion of the wide-character array that
745 will display in the number of columns of screen display specified by precision
746 will be written. If the precision is not specified, it is taken to be infinite,
747 so all wide characters up to the first null character are printed. An argument
748 with a null value will yield undefined results.
757 The argument must be a pointer to \fBvoid\fR. The value of the pointer is
758 converted to a set of sequences of printable characters, which should be the
759 same as the set of sequences that are matched by the \fB%p\fR conversion of the
760 \fBscanf\fR(3C) function.
769 The argument must be a pointer to an integer into which is written the number
770 of bytes written to the output standard I/O stream so far by this call to one
771 of the \fBprintf()\fR functions. No argument is converted.
780 Print a \fB%\fR; no argument is converted. The entire conversion specification
786 If a conversion specification does not match one of the above forms, the
787 behavior is undefined.
790 In no case does a non-existent or small field width cause truncation of a
791 field; if the result of a conversion is wider than the field width, the field
792 is simply expanded to contain the conversion result. Characters generated by
793 \fBprintf()\fR and \fBfprintf()\fR are printed as if the \fBputc\fR(3C)
794 function had been called.
797 The \fBst_ctime\fR and \fBst_mtime\fR fields of the file will be marked for
798 update between the call to a successful execution of \fBprintf()\fR or
799 \fBfprintf()\fR and the next successful completion of a call to
800 \fBfflush\fR(3C) or \fBfclose\fR(3C) on the same stream or a call to
801 \fBexit\fR(3C) or \fBabort\fR(3C).
805 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBasprintf()\fR
806 functions return the number of bytes transmitted (excluding the terminating
807 null byte in the case of \fBsprintf()\fR and \fBasprintf()\fR).
810 The \fBsnprintf()\fR function returns the number of bytes that would have been
811 written to \fIs\fR if \fIn\fR had been sufficiently large (excluding the
812 terminating null byte.) If the value of \fIn\fR is 0 on a call to
813 \fBsnprintf()\fR, \fIs\fR can be a null pointer and the number of bytes that
814 would have been written if \fIn\fR had been sufficiently large (excluding the
815 terminating null byte) is returned.
818 Each function returns a negative value if an output error was encountered.
822 For the conditions under which \fBprintf()\fR and \fBfprintf()\fR will fail and
823 may fail, refer to \fBfputc\fR(3C) or \fBfputwc\fR(3C).
826 The \fBsnprintf()\fR function will fail if:
830 \fB\fBEOVERFLOW\fR\fR
833 The value of \fIn\fR is greater than \fBINT_MAX\fR or the number of bytes
834 needed to hold the output excluding the terminating null is greater than
840 The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR
841 functions may fail if:
848 A wide-character code that does not correspond to a valid character has been
858 There are insufficient arguments.
863 The \fBprintf()\fR, \fBfprintf()\fR, and \fBasprintf()\fR functions may fail
864 due to an underlying \fBmalloc\fR(3C) failure if:
871 Storage space is temporarily unavailable.
880 Insufficient storage space is available.
886 If the application calling the \fBprintf()\fR functions has any objects of type
887 \fBwint_t\fR or \fBwchar_t\fR, it must also include the header \fB<wchar.h>\fR
888 to have these objects defined.
889 .SS "Escape Character Sequences"
892 It is common to use the following escape sequences built into the C language
893 when entering format strings for the \fBprintf()\fR functions, but these
894 sequences are processed by the C compiler, not by the \fBprintf()\fR function.
901 Alert. Ring the bell.
910 Backspace. Move the printing position to one character before the current
911 position, unless the current position is the start of a line.
920 Form feed. Move the printing position to the initial printing position of the
930 Newline. Move the printing position to the start of the next line.
939 Carriage return. Move the printing position to the start of the current line.
948 Horizontal tab. Move the printing position to the next implementation-defined
949 horizontal tab position on the current line.
958 Vertical tab. Move the printing position to the start of the next
959 implementation-defined vertical tab position.
964 In addition, the C language supports character sequences of the form
976 which translates into the character represented by the octal or hexadecimal
977 number. For example, if ASCII representations are being used, the letter 'a'
978 may be written as '\e141' and 'Z' as '\e132'. This syntax is most frequently
979 used to represent the null character as '\e0'. This is exactly equivalent to
980 the numeric constant zero (0). Note that the octal number does not include the
981 zero prefix as it would for a normal octal constant. To specify a hexadecimal
982 number, omit the zero so that the prefix is an 'x' (uppercase 'X' is not
983 allowed in this context). Support for hexadecimal sequences is an ANSI
984 extension. See \fBstandards\fR(5).
987 \fBExample 1 \fRTo print the language-independent date and time format, the
988 following statement could be used:
992 \fBprintf (format, weekday, month, day, hour, min);\fR
998 For American usage, \fIformat\fR could be a pointer to the string:
1003 \fB"%s, %s %d, %d:%.2d\en"\fR
1009 producing the message:
1014 \fBSunday, July 3, 10:02\fR
1020 whereas for German usage, \fIformat\fR could be a pointer to the string:
1025 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
1031 producing the message:
1036 Sonntag, 3. Juli, 10:02
1041 \fBExample 2 \fRTo print a date and time in the form \fBSunday, July 3,
1042 10:02\fR, where \fBweekday\fR and \fBmonth\fR are pointers to null-terminated
1047 printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min);
1052 \fBExample 3 \fRTo print pi to 5 decimal places:
1056 printf("pi = %.5f", 4 * atan(1.0));
1062 \fBExample 4 \fRThe following example applies only to applications that are not
1063 standard-conforming. To print a list of names in columns which are 20
1068 \fBprintf("%20s%20s%20s", lastname, firstname, middlename);\fR
1075 See \fBattributes\fR(5) for descriptions of the following attributes:
1083 ATTRIBUTE TYPE ATTRIBUTE VALUE
1087 Interface Stability Committed
1096 All of these functions can be used safely in multithreaded applications, as
1097 long as \fBsetlocale\fR(3C) is not being called to change the locale. The
1098 \fBsprintf()\fR and \fBsnprintf()\fR functions are Async-Signal-Safe.
1101 See \fBstandards\fR(5) for the standards conformance of \fBprintf()\fR,
1102 \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR. The \fBasprintf()\fR
1103 function is modeled on the one that appears in the FreeBSD, NetBSD, and GNU C
1108 \fBexit\fR(2), \fBlseek\fR(2), \fBwrite\fR(2), \fBabort\fR(3C), \fBecvt\fR(3C),
1109 \fBexit\fR(3C), \fBfclose\fR(3C), \fBfflush\fR(3C), \fBfputwc\fR(3C),
1110 \fBfree\fR(3C), \fBmalloc\fR(3C), \fBputc\fR(3C), \fBscanf\fR(3C),
1111 \fBsetlocale\fR(3C), \fBstdio\fR(3C), \fBvprintf\fR(3C), \fBwcstombs\fR(3C),
1112 \fBwctomb\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), \fBstandards\fR(5)
1116 If the \fBj\fR length modifier is used, 32-bit applications that were compiled
1117 using \fBc89\fR on releases prior to Solaris 10 will experience undefined
1121 The \fBsnprintf()\fR return value when \fIn\fR = 0 was changed in the Solaris
1122 10 release. The change was based on the SUSv3 specification. The previous
1123 behavior was based on the initial SUSv2 specification, where \fBsnprintf()\fR
1124 when \fIn\fR = 0 returns an unspecified value less than 1.