Print this page
12953 convert printf(3c) and vprintf(3c) to mdoc
Change-Id: Iffbfe29bbd443e1a7ad0fc33fd7a7851428a3dc8

*** 43,1124 **** .\" Copyright 1989 AT&T .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved. .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved. .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved. .\" ! .TH PRINTF 3C "Jan 7, 2009" ! .SH NAME ! printf, fprintf, sprintf, snprintf, asprintf \- print formatted output ! .SH SYNOPSIS ! .LP ! .nf ! #include <stdio.h> ! ! \fBint\fR \fBprintf\fR(\fBconst char *restrict\fR \fIformat\fR, ! \fB/*\fR \fIargs\fR*/ ...); ! .fi ! ! .LP ! .nf ! \fBint\fR \fBfprintf\fR(\fBFILE *restrict\fR \fIstream\fR, \fBconst char *restrict\fR \fIformat\fR, ! \fB/*\fR \fIargs\fR*/ ...); ! .fi ! ! .LP ! .nf ! \fBint\fR \fBsprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBconst char *restrict\fR \fIformat\fR, ! \fB/*\fR \fIargs\fR*/ ...); ! .fi ! ! .LP ! .nf ! \fBint\fR \fBsnprintf\fR(\fBchar *restrict\fR \fIs\fR, \fBsize_t\fR \fIn\fR, ! \fBconst char *restrict\fR \fIformat\fR, \fB/*\fR \fIargs\fR*/ ...); ! .fi ! ! .LP ! .nf ! \fBint\fR \fBasprintf\fR(\fBchar **\fR \fIret\fR, \fBconst char *restrict\fR \fIformat\fR, ! \fB/*\fR \fIargs\fR*/ ...); ! .fi ! ! .SH DESCRIPTION ! .sp ! .LP ! The \fBprintf()\fR function places output on the standard output stream ! \fBstdout\fR. ! .sp ! .LP ! The \fBfprintf()\fR function places output on on the named output stream ! \fIstream\fR. ! .sp ! .LP ! The \fBsprintf()\fR function places output, followed by the null byte ! (\fB\e0\fR), in consecutive bytes starting at \fIs\fR; it is the user's ! responsibility to ensure that enough storage is available. ! .sp ! .LP ! The \fBsnprintf()\fR function is identical to \fBsprintf()\fR with the addition ! of the argument \fIn\fR, which specifies the size of the buffer referred to by ! \fIs\fR. If \fIn\fR is 0, nothing is written and \fIs\fR can be a null pointer. ! Otherwise, output bytes beyond the \fIn\fR-1st are discarded instead of being ! written to the array and a null byte is written at the end of the bytes ! actually written into the array. ! .sp ! .LP ! The \fBasprintf()\fR function is the same as the \fBsprintf()\fR function ! except that it returns, in the \fIret\fR argument, a pointer to a buffer ! sufficiently large to hold the output string. This pointer should be passed to ! \fBfree\fR(3C) to release the allocated storage when it is no longer needed. If ! sufficient space cannot be allocated, the \fBasprintf()\fR function returns -1 ! and sets \fIret\fR to be a \fINULL\fR pointer. ! .sp ! .LP Each of these functions converts, formats, and prints its arguments under ! control of the \fIformat\fR. The \fIformat\fR is a character string, beginning ! and ending in its initial shift state, if any. The \fIformat\fR is composed of ! zero or more directives: \fBordinary characters\fR, which are simply copied to ! the output stream and \fBconversion specifications\fR, each of which results in ! the fetching of zero or more arguments. The results are undefined if there are ! insufficient arguments for the \fIformat\fR. If the \fIformat\fR is exhausted ! while arguments remain, the excess arguments are evaluated but are otherwise ! ignored. ! .sp ! .LP ! Conversions can be applied to the \fIn\fRth argument after the \fIformat\fR in ! the argument list, rather than to the next unused argument. In this case, the ! conversion specifier \fB%\fR (see below) is replaced by the sequence ! \fB%\fR\fIn\fR\fB$\fR, where \fIn\fR is a decimal integer in the range [1, ! \fBNL_ARGMAX\fR], giving the position of the argument in the argument list. This feature provides for the definition of format strings that select ! arguments in an order appropriate to specific languages (see the \fBEXAMPLES\fR section). ! .sp ! .LP ! In format strings containing the \fB%\fR\fIn\fR\fB$\fR form of conversion ! specifications, numbered arguments in the argument list can be referenced from ! the format string as many times as required. ! .sp ! .LP ! In format strings containing the \fB%\fR form of conversion specifications, ! each argument in the argument list is used exactly once. ! .sp ! .LP ! All forms of the \fBprintf()\fR functions allow for the insertion of a ! language-dependent radix character in the output string. The radix character is ! defined by the program's locale (category \fBLC_NUMERIC\fR). In the POSIX ! locale, or in a locale where the radix character is not defined, the radix ! character defaults to a period (\fB\&.\fR). ! .SS "Conversion Specifications" ! .sp ! .LP ! Each conversion specification is introduced by the \fB%\fR character or by the ! character sequence \fB%\fR\fIn\fR\fB$\fR, after which the following appear in ! sequence: ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! An optional field, consisting of a decimal digit string followed by a \fB$\fR, ! specifying the next argument to be converted. If this field is not provided, ! the \fIargs\fR following the last argument converted will be used. ! .RE ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! Zero or more \fIflags\fR (in any order), which modify the meaning of the conversion specification. ! .RE ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! An optional minimum \fIfield width\fR. If the converted value has fewer bytes ! than the field width, it will be padded with spaces by default on the left; it ! will be padded on the right, if the left-adjustment flag (\fB\(hy\fR), ! described below, is given to the field width. The field width takes the form of ! an asterisk (*), described below, or a decimal integer. ! .sp ! If the conversion specifier is \fBs\fR, a standard-conforming application (see ! \fBstandards\fR(5)) interprets the field width as the minimum number of bytes ! to be printed; an application that is not standard-conforming interprets the ! field width as the minimum number of columns of screen display. For an ! application that is not standard-conforming, \fB%10s\fR means if the converted ! value has a screen width of 7 columns, 3 spaces would be padded on the right. ! .sp ! If the format is \fB%ws\fR, then the field width should be interpreted as the minimum number of columns of screen display. ! .RE ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! An optional \fIprecision\fR that gives the minimum number of digits to appear ! for the \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, and \fBX\fR conversions ! (the field is padded with leading zeros); the number of digits to appear after ! the radix character for the \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, and ! \fBF\fR conversions, the maximum number of significant digits for the \fBg\fR ! and \fBG\fR conversions; or the maximum number of bytes to be printed from a ! string in \fBs\fR and \fBS\fR conversions. The precision takes the form of a ! period (.) followed either by an asterisk (*), described below, or an optional ! decimal digit string, where a null digit string is treated as 0. If a precision ! appears with any other conversion specifier, the behavior is undefined. ! .sp ! If the conversion specifier is \fBs\fR or \fBS\fR, a standard-conforming ! application (see \fBstandards\fR(5)) interprets the precision as the maximum ! number of bytes to be written; an application that is not standard-conforming ! interprets the precision as the maximum number of columns of screen display. ! For an application that is not standard-conforming, \fB%.5s\fR would print only ! the portion of the string that would display in 5 screen columns. Only complete ! characters are written. ! .sp ! For \fB%ws\fR, the precision should be interpreted as the maximum number of ! columns of screen display. The precision takes the form of a period (\fB\&.\fR) followed by a decimal digit string; a null digit string is treated as zero. Padding specified by the precision overrides the padding specified by the field width. ! .RE ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! An optional \fIlength modifier\fR that specified the size of the argument. ! .RE ! .RS +4 ! .TP ! .ie t \(bu ! .el o ! A \fIconversion specifier\fR that indicates the type of conversion to be ! applied. ! .RE ! .sp ! .LP A field width, or precision, or both can be indicated by an asterisk ! (\fB*\fR) . In this case, an argument of type \fBint\fR supplies the field width or ! precision. Arguments specifying field width, or precision, or both must appear ! in that order before the argument, if any, to be converted. A negative field ! width is taken as a \(mi flag followed by a positive field width. A negative ! precision is taken as if the precision were omitted. In format strings ! containing the \fB%\fR\fIn\fR\fB$\fR form of a conversion specification, a ! field width or precision may be indicated by the sequence ! \fB*\fR\fIm\fR\fB$\fR, where \fIm\fR is a decimal integer in the range [1, ! \fBNL_ARGMAX\fR] giving the position in the argument list (after the format ! argument) of an integer argument containing the field width or precision, for ! example: ! .sp ! .in +2 ! .nf ! printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec); ! .fi ! .in -2 ! ! .sp ! .LP ! The \fIformat\fR can contain either numbered argument specifications (that is, ! \fB%\fR\fIn\fR\fB$\fR and \fB*\fR\fIm\fR\fB$\fR), or unnumbered argument ! specifications (that is, \fB%\fR and \fB*\fR), but normally not both. The only ! exception to this is that \fB%%\fR can be mixed with the \fB%\fR\fIn\fR\fB$\fR ! form. The results of mixing numbered and unnumbered argument specifications in ! a \fIformat\fR string are undefined. When numbered argument specifications are ! used, specifying the \fIN\fRth argument requires that all the leading ! arguments, from the first to the (\fIN-1\fR)th, are specified in the format ! string. ! .SS "Flag Characters" ! .sp ! .LP The flag characters and their meanings are: ! .sp ! .ne 2 ! .na ! \fB\fB\&'\fR\fR ! .ad ! .RS 9n ! The integer portion of the result of a decimal conversion (\fB%i\fR, \fB%d\fR, ! \fB%u\fR, \fB%f\fR, \fB%F\fR, \fB%g\fR, or \fB%G\fR) will be formatted with ! thousands' grouping characters. For other conversions the behavior is ! undefined. The non-monetary grouping character is used. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\(mi\fR\fR ! .ad ! .RS 9n ! The result of the conversion will be left-justified within the field. The ! conversion will be right-justified if this flag is not specified. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB+\fR\fR ! .ad ! .RS 9n ! The result of a signed conversion will always begin with a sign (+ or -). The ! conversion will begin with a sign only when a negative value is converted if this flag is not specified. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBspace\fR\fR ! .ad ! .RS 9n If the first character of a signed conversion is not a sign or if a signed conversion results in no characters, a space will be placed before the result. ! This means that if the \fBspace\fR and \fB+\fR flags both appear, the space ! flag will be ignored. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB#\fR\fR ! .ad ! .RS 9n ! The value is to be converted to an alternate form. For \fBc\fR, \fBd\fR, ! \fBi\fR, \fBs\fR, and \fBu\fR conversions, the flag has no effect. For an ! \fBo\fR conversion, it increases the precision (if necessary) to force the ! first digit of the result to be a zero. For \fBx\fR or \fBX\fR conversion, a ! non-zero result will have \fB0x\fR (or \fB0X\fR) prepended to it. For \fBa\fR, ! \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions, ! the result will always contain a radix character, even if no digits follow the ! radix character. Without this flag, the radix character appears in the result ! of these conversions only if a digit follows it. For \fBg\fR and \fBG\fR conversions, trailing zeros will not be removed from the result as they normally are. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB0\fR\fR ! .ad ! .RS 9n ! For \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, \fBX\fR, \fBa\fR, \fBA\fR, ! \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, and \fBG\fR conversions, leading ! zeros (following any indication of sign or base) are used to pad to the field ! width; no space padding is performed. If the \fB0\fR and \fB\(mi\fR flags both ! appear, the \fB0\fR flag will be ignored. For \fBd\fR, \fBi\fR, \fBo\fR, ! \fBu\fR, \fBx\fR, and \fBX\fR conversions, if a precision is specified, the ! \fB0\fR flag will be ignored. If the \fB0\fR and \fB\&'\fR flags both appear, ! the grouping characters are inserted before zero padding. For other ! conversions, the behavior is undefined. ! .RE ! ! .SS "Length Modifiers" ! .sp ! .LP The length modifiers and their meanings are: ! .sp ! .ne 2 ! .na ! \fB\fBhh\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBsigned char\fR or \fBunsigned ! char\fR argument (the argument will have been promoted according to the integer ! promotions, but its value will be converted to \fBsigned char\fR or \fBunsigned ! char\fR before printing); or that a following \fBn\fR conversion specifier ! applies to a pointer to a \fBsigned char\fR argument. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBh\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBshort\fR or \fBunsigned short\fR argument (the argument will have been promoted according to the integer ! promotions, but its value will be converted to \fBshort\fR or \fBunsigned ! short\fR before printing); or that a following \fBn\fR conversion specifier ! applies to a pointer to a \fBshort\fR argument. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBl (ell)\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBlong\fR or \fBunsigned long\fR ! argument; that a following \fBn\fR conversion specifier applies to a pointer to ! a \fBlong\fR argument; that a following \fBc\fR conversion specifier applies to ! a \fBwint_t\fR argument; that a following \fBs\fR conversion specifier applies ! to a pointer to a \fBwchar_t\fR argument; or has no effect on a following ! \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, \fBF\fR, \fBg\fR, or \fBG\fR conversion specifier. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBll (ell-ell)\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBlong long\fR or \fBunsigned long ! long\fR argument; or that a following \fBn\fR conversion specifier applies to a ! pointer to a \fBlong long\fR argument. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBj\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to an \fBintmax_t\fR or \fBuintmax_t\fR ! argument; or that a following \fBn\fR conversion specifier applies to a pointer ! to an \fBintmax_t\fR argument. See NOTES. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBz\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBsize_t\fR or the corresponding ! signed integer type argument; or that a following \fBn\fR conversion specifier ! applies to a pointer to a signed integer type corresponding to \fBsize_t\fR argument. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBt\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBd\fR, \fBi\fR, \fBo\fR, \fBu\fR, \fBx\fR, or ! \fBX\fR conversion specifier applies to a \fBptrdiff_t\fR or the corresponding ! unsigned type argument; or that a following n conversion specifier applies to a ! pointer to a \fBptrdiff_t\fR argument. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBL\fR\fR ! .ad ! .RS 16n ! Specifies that a following \fBa\fR, \fBA\fR, \fBe\fR, \fBE\fR, \fBf\fR, ! \fBF\fR, \fBg\fR, or \fBG\fR conversion specifier applies to a \fBlong ! double\fR argument. ! .RE ! ! .sp ! .LP If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined. ! .SS "Conversion Specifiers" ! .sp ! .LP ! Each conversion specifier results in fetching zero or more arguments. The ! results are undefined if there are insufficient arguments for the format. If ! the format is exhausted while arguments remain, the excess arguments are ignored. ! .sp ! .LP The conversion specifiers and their meanings are: ! .sp ! .ne 2 ! .na ! \fB\fBd\fR, \fBi\fR\fR ! .ad ! .RS 8n ! The \fBint\fR argument is converted to a signed decimal in the style ! \fB[\fR\(mi\fB]\fR\fIdddd\fR. The precision specifies the minimum number of ! digits to appear; if the value being converted can be represented in fewer ! digits, it will be expanded with leading zeros. The default precision is 1. The ! result of converting 0 with an explicit precision of 0 is no characters. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBo\fR\fR ! .ad ! .RS 8n ! The \fBunsigned int\fR argument is converted to unsigned octal format in the ! style \fIdddd\fR. The precision specifies the minimum number of digits to ! appear; if the value being converted can be represented in fewer digits, it ! will be expanded with leading zeros. The default precision is 1. The result of ! converting 0 with an explicit precision of 0 is no characters. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBu\fR\fR ! .ad ! .RS 8n ! The \fBunsigned int\fR argument is converted to unsigned decimal format in the ! style \fIdddd\fR. The precision specifies the minimum number of digits to ! appear; if the value being converted can be represented in fewer digits, it ! will be expanded with leading zeros. The default precision is 1. The result of ! converting 0 with an explicit precision of 0 is no characters. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBx\fR\fR ! .ad ! .RS 8n ! The \fBunsigned int\fR argument is converted to unsigned hexadecimal format in ! the style \fIdddd\fR; the letters \fBabcdef\fR are used. The precision ! specifies the minimum number of digits to appear; if the value being converted ! can be represented in fewer digits, it will be expanded with leading zeros. The ! default precision is 1. The result of converting 0 with an explicit precision ! of 0 is no characters. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBX\fR\fR ! .ad ! .RS 8n ! Behaves the same as the \fBx\fR conversion specifier except that letters ! \fBABCDEF\fR are used instead of \fBabcdef\fR. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBf\fR, \fBF\fR\fR ! .ad ! .RS 8n ! The \fBdouble\fR argument is converted to decimal notation in the style ! [\fB\(mi\fR]\fIddd\fR\fB\&.\fR\fIddd\fR, where the number of digits after the ! radix character (see \fBsetlocale\fR(3C)) is equal to the precision ! specification. If the precision is missing it is taken as 6; if the precision ! is explicitly 0 and the \fB#\fR flag is not specified, no radix character ! appears. If a radix character appears, at least 1 digit appears before it. The ! converted value is rounded to fit the specified output format according to the ! prevailing floating point rounding direction mode. If the conversion is not ! exact, an inexact exception is raised. ! .sp ! For the \fBf\fR specifier, a double argument representing an infinity or NaN is ! converted in the style of the \fBe\fR conversion specifier, except that for an ! infinite argument, "infinity" or "Infinity" is printed when the precision is at ! least 8 and "inf" or "Inf" is printed otherwise. ! .sp ! For the F specifier, a double argument representing an infinity or NaN is ! converted in the SUSv3 style of the E conversion specifier, except that for an ! infinite argument, "INFINITY" is printed when the precision is at least 8 and ! or "INF" is printed otherwise. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBe\fR, \fBE\fR\fR ! .ad ! .RS 8n ! The \fBdouble\fR argument is converted to the style ! [\fB\(mi\fR]\fId\fR\fB\&.\fR\fIddd\fR\fBe\fR\fI\(+-dd\fR, where there is one ! digit before the radix character (which is non-zero if the argument is ! non-zero) and the number of digits after it is equal to the precision. When the ! precision is missing it is taken as 6; if the precision is 0 and the \fB#\fR ! flag is not specified, no radix character appears. The \fBE\fR conversion ! specifier will produce a number with \fBE\fR instead of \fBe\fR introducing the ! exponent. The exponent always contains at least two digits. The converted value ! is rounded to fit the specified output format according to the prevailing ! floating point rounding direction mode. If the conversion is not exact, an ! inexact exception is raised. ! .sp Infinity and NaN values are handled in one of the following ways: ! .sp ! .ne 2 ! .na ! \fBSUSv3\fR ! .ad ! .RS 11n ! For the \fBe\fR specifier, a \fBdouble\fR argument representing an infinity is ! printed as "[\(mi]\fBinfinity\fR", when the precision for the conversion is at ! least 7 and as "[\(mi]\fBinf\fR" otherwise. A \fBdouble\fR argument ! representing a NaN is printed as "[\(mi]\fBnan\fR". For the \fBE\fR specifier, ! "\fBINF\fR", "\fBINFINITY\fR", and "\fBNAN\fR" are printed instead of ! "\fBinf\fR", "\fBinfinity\fR", and "\fBnan\fR", respectively. Printing of the ! sign follows the rules described above. ! .RE ! ! .sp ! .ne 2 ! .na ! \fBDefault\fR ! .ad ! .RS 11n ! A \fBdouble\fR argument representing an infinity is printed as ! "[\(mi]\fBInfinity\fR", when the precision for the conversion is at least 7 and ! as "[\(mi]\fBInf\fR" otherwise. A double argument representing a NaN is printed ! as "[\(mi]\fBNaN\fR". Printing of the sign follows the rules described above. ! .RE ! ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBg\fR, \fBG\fR\fR ! .ad ! .RS 8n ! The \fBdouble\fR argument is printed in style \fBf\fR or \fBe\fR (or in style ! \fBE\fR in the case of a \fBG\fR conversion specifier), with the precision ! specifying the number of significant digits. If an explicit precision is 0, it ! is taken as 1. The style used depends on the value converted: style \fBe\fR (or ! \fBE\fR) will be used only if the exponent resulting from the conversion is ! less than -4 or greater than or equal to the precision. Trailing zeros are ! removed from the fractional part of the result. A radix character appears only ! if it is followed by a digit. ! .sp ! A \fBdouble\fR argument representing an infinity or NaN is converted in the ! style of the \fBe\fR or \fBE\fR conversion specifier, except that for an ! infinite argument, "infinity", "INFINITY", or "Infinity" is printed when the ! precision is at least 8 and "inf", "INF", or "Inf" is printed otherwise. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBa\fR, \fBA\fR\fR ! .ad ! .RS 8n ! A \fBdouble\fR argument representing a floating-point number is converted in ! the style "[-]0\fIxh\fR.\fIhhhhp\fR\(+-\fId\fR", where the single hexadecimal ! digit preceding the radix point is 0 if the value converted is zero and 1 ! otherwise and the number of hexadecimal digits after it is equal to the ! precision; if the precision is missing, the number of digits printed after the ! radix point is 13 for the conversion of a double value, 16 for the conversion ! of a long double value on x86, and 28 for the conversion of a long double value ! on SPARC; if the precision is zero and the '#' flag is not specified, no ! decimal-point character will appear. The letters "\fBabcdef\fR" are used for ! \fBa\fR conversion and the letters "\fBABCDEF\fR" for \fBA\fR conversion. The ! \fBA\fR conversion specifier produces a number with '\fBX\fR' and '\fBP\fR' ! instead of '\fBx\fR' and '\fBp\fR'. The exponent will always contain at least ! one digit, and only as many more digits as necessary to represent the decimal ! exponent of 2. If the value is zero, the exponent is zero. ! .sp The converted value is rounded to fit the specified output format according to ! the prevailing floating point rounding direction mode. If the conversion is not ! exact, an inexact exception is raised. ! .sp ! A \fBdouble\fR argument representing an infinity or NaN is converted in the ! SUSv3 style of an \fBe\fR or \fBE\fR conversion specifier. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBc\fR\fR ! .ad ! .RS 8n ! The \fBint\fR argument is converted to an \fBunsigned char\fR, and the ! resulting byte is printed. ! .sp ! If an \fBl\fR (ell) qualifier is present, the \fBwint_t\fR argument is ! converted as if by an \fBls\fR conversion specification with no precision and ! an argument that points to a two-element array of type \fBwchar_t\fR, the first ! element of which contains the \fBwint_t\fR argument to the \fBls\fR conversion ! specification and the second element contains a null wide-character. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBC\fR\fR ! .ad ! .RS 8n ! Same as \fBlc\fR. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBwc\fR\fR ! .ad ! .RS 8n ! The \fBint\fR argument is converted to a wide character (\fBwchar_t\fR), and ! the resulting wide character is printed. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBs\fR\fR ! .ad ! .RS 8n ! The argument must be a pointer to an array of \fBchar\fR. Bytes from the array ! are written up to (but not including) any terminating null byte. If a precision ! is specified, a standard-conforming application (see \fBstandards\fR(5)) will ! write only the number of bytes specified by precision; an application that is ! not standard-conforming will write only the portion of the string that will ! display in the number of columns of screen display specified by precision. If ! the precision is not specified, it is taken to be infinite, so all bytes up to ! the first null byte are printed. An argument with a null value will yield ! undefined results. ! .sp ! If an \fBl\fR (ell) qualifier is present, the argument must be a pointer to an ! array of type \fBwchar_t\fR. Wide-characters from the array are converted to ! characters (each as if by a call to the \fBwcrtomb\fR(3C) function, with the ! conversion state described by an \fBmbstate_t\fR object initialized to zero ! before the first wide-character is converted) up to and including a terminating ! null wide-character. The resulting characters are written up to (but not ! including) the terminating null character (byte). If no precision is specified, ! the array must contain a null wide-character. If a precision is specified, no ! more than that many characters (bytes) are written (including shift sequences, ! if any), and the array must contain a null wide-character if, to equal the ! character sequence length given by the precision, the function would need to ! access a wide-character one past the end of the array. In no case is a partial ! character written. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBS\fR\fR ! .ad ! .RS 8n ! Same as \fBls\fR. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBws\fR\fR ! .ad ! .RS 8n ! The argument must be a pointer to an array of \fBwchar_t\fR. Bytes from the ! array are written up to (but not including) any terminating null character. If ! the precision is specified, only that portion of the wide-character array that ! will display in the number of columns of screen display specified by precision ! will be written. If the precision is not specified, it is taken to be infinite, ! so all wide characters up to the first null character are printed. An argument ! with a null value will yield undefined results. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBp\fR\fR ! .ad ! .RS 8n ! The argument must be a pointer to \fBvoid\fR. The value of the pointer is ! converted to a set of sequences of printable characters, which should be the ! same as the set of sequences that are matched by the \fB%p\fR conversion of the ! \fBscanf\fR(3C) function. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBn\fR\fR ! .ad ! .RS 8n The argument must be a pointer to an integer into which is written the number of bytes written to the output standard I/O stream so far by this call to one ! of the \fBprintf()\fR functions. No argument is converted. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB%\fR\fR ! .ad ! .RS 8n ! Print a \fB%\fR; no argument is converted. The entire conversion specification ! must be %%. ! .RE ! ! .sp ! .LP If a conversion specification does not match one of the above forms, the behavior is undefined. ! .sp ! .LP In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field ! is simply expanded to contain the conversion result. Characters generated by ! \fBprintf()\fR and \fBfprintf()\fR are printed as if the \fBputc\fR(3C) function had been called. ! .sp ! .LP ! The \fBst_ctime\fR and \fBst_mtime\fR fields of the file will be marked for ! update between the call to a successful execution of \fBprintf()\fR or ! \fBfprintf()\fR and the next successful completion of a call to ! \fBfflush\fR(3C) or \fBfclose\fR(3C) on the same stream or a call to ! \fBexit\fR(3C) or \fBabort\fR(3C). ! .SH RETURN VALUES ! .sp ! .LP ! The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBasprintf()\fR functions return the number of bytes transmitted (excluding the terminating ! null byte in the case of \fBsprintf()\fR and \fBasprintf()\fR). ! .sp ! .LP ! The \fBsnprintf()\fR function returns the number of bytes that would have been ! written to \fIs\fR if \fIn\fR had been sufficiently large (excluding the ! terminating null byte.) If the value of \fIn\fR is 0 on a call to ! \fBsnprintf()\fR, \fIs\fR can be a null pointer and the number of bytes that ! would have been written if \fIn\fR had been sufficiently large (excluding the ! terminating null byte) is returned. ! .sp ! .LP Each function returns a negative value if an output error was encountered. ! .SH ERRORS ! .sp ! .LP ! For the conditions under which \fBprintf()\fR and \fBfprintf()\fR will fail and ! may fail, refer to \fBfputc\fR(3C) or \fBfputwc\fR(3C). ! .sp ! .LP ! The \fBsnprintf()\fR function will fail if: ! .sp ! .ne 2 ! .na ! \fB\fBEOVERFLOW\fR\fR ! .ad ! .RS 13n ! The value of \fIn\fR is greater than \fBINT_MAX\fR or the number of bytes ! needed to hold the output excluding the terminating null is greater than ! \fBINT_MAX\fR. ! .RE ! ! .sp ! .LP ! The \fBprintf()\fR, \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR ! functions may fail if: ! .sp ! .ne 2 ! .na ! \fB\fBEILSEQ\fR\fR ! .ad ! .RS 10n ! A wide-character code that does not correspond to a valid character has been ! detected. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBEINVAL\fR\fR ! .ad ! .RS 10n ! There are insufficient arguments. ! .RE ! ! .sp ! .LP ! The \fBprintf()\fR, \fBfprintf()\fR, and \fBasprintf()\fR functions may fail ! due to an underlying \fBmalloc\fR(3C) failure if: ! .sp ! .ne 2 ! .na ! \fB\fBEAGAIN\fR\fR ! .ad ! .RS 10n ! Storage space is temporarily unavailable. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fBENOMEM\fR\fR ! .ad ! .RS 10n ! Insufficient storage space is available. ! .RE ! ! .SH USAGE ! .sp ! .LP ! If the application calling the \fBprintf()\fR functions has any objects of type ! \fBwint_t\fR or \fBwchar_t\fR, it must also include the header \fB<wchar.h>\fR to have these objects defined. ! .SS "Escape Character Sequences" ! .sp ! .LP It is common to use the following escape sequences built into the C language ! when entering format strings for the \fBprintf()\fR functions, but these ! sequences are processed by the C compiler, not by the \fBprintf()\fR function. ! .sp ! .ne 2 ! .na ! \fB\fB\ea\fR\fR ! .ad ! .RS 7n ! Alert. Ring the bell. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\eb\fR\fR ! .ad ! .RS 7n ! Backspace. Move the printing position to one character before the current ! position, unless the current position is the start of a line. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\ef\fR\fR ! .ad ! .RS 7n ! Form feed. Move the printing position to the initial printing position of the ! next logical page. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\en\fR\fR ! .ad ! .RS 7n ! Newline. Move the printing position to the start of the next line. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\er\fR\fR ! .ad ! .RS 7n ! Carriage return. Move the printing position to the start of the current line. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\et\fR\fR ! .ad ! .RS 7n ! Horizontal tab. Move the printing position to the next implementation-defined ! horizontal tab position on the current line. ! .RE ! ! .sp ! .ne 2 ! .na ! \fB\fB\ev\fR\fR ! .ad ! .RS 7n ! Vertical tab. Move the printing position to the start of the next ! implementation-defined vertical tab position. ! .RE ! ! .sp ! .LP In addition, the C language supports character sequences of the form ! .sp ! .LP ! \eoctal-number ! .sp ! .LP and ! .sp ! .LP ! \ehex-number ! .sp ! .LP which translates into the character represented by the octal or hexadecimal ! number. For example, if ASCII representations are being used, the letter 'a' ! may be written as '\e141' and 'Z' as '\e132'. This syntax is most frequently ! used to represent the null character as '\e0'. This is exactly equivalent to ! the numeric constant zero (0). Note that the octal number does not include the ! zero prefix as it would for a normal octal constant. To specify a hexadecimal ! number, omit the zero so that the prefix is an 'x' (uppercase 'X' is not ! allowed in this context). Support for hexadecimal sequences is an ANSI ! extension. See \fBstandards\fR(5). ! .SH EXAMPLES ! .LP ! \fBExample 1 \fRTo print the language-independent date and time format, the following statement could be used: ! .sp ! .in +2 ! .nf ! \fBprintf (format, weekday, month, day, hour, min);\fR ! .fi ! .in -2 ! ! .sp ! .LP ! For American usage, \fIformat\fR could be a pointer to the string: ! ! .sp ! .in +2 ! .nf ! \fB"%s, %s %d, %d:%.2d\en"\fR ! .fi ! .in -2 ! ! .sp ! .LP producing the message: ! ! .sp ! .in +2 ! .nf ! \fBSunday, July 3, 10:02\fR ! .fi ! .in -2 ! ! .sp ! .LP ! whereas for German usage, \fIformat\fR could be a pointer to the string: ! ! .sp ! .in +2 ! .nf ! "%1$s, %3$d. %2$s, %4$d:%5$.2d\en" ! .fi ! .in -2 ! ! .sp ! .LP producing the message: ! ! .sp ! .in +2 ! .nf ! Sonntag, 3. Juli, 10:02 ! .fi ! .in -2 ! ! .LP ! \fBExample 2 \fRTo print a date and time in the form \fBSunday, July 3, ! 10:02\fR, where \fBweekday\fR and \fBmonth\fR are pointers to null-terminated ! strings: ! .sp ! .in +2 ! .nf ! printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min); ! .fi ! .in -2 ! ! .LP ! \fBExample 3 \fRTo print pi to 5 decimal places: ! .sp ! .in +2 ! .nf ! printf("pi = %.5f", 4 * atan(1.0)); ! .fi ! .in -2 ! ! .SS "Default" ! .LP ! \fBExample 4 \fRThe following example applies only to applications that are not ! standard-conforming. To print a list of names in columns which are 20 ! characters wide: ! .sp ! .in +2 ! .nf ! \fBprintf("%20s%20s%20s", lastname, firstname, middlename);\fR ! .fi ! .in -2 ! ! .SH ATTRIBUTES ! .sp ! .LP ! See \fBattributes\fR(5) for descriptions of the following attributes: ! .sp ! ! .sp ! .TS ! box; ! l | l ! l | l . ! ATTRIBUTE TYPE ATTRIBUTE VALUE ! _ ! CSI Enabled ! _ ! Interface Stability Committed ! _ ! MT-Level See below. ! _ ! Standard See below. ! .TE ! ! .sp ! .LP All of these functions can be used safely in multithreaded applications, as ! long as \fBsetlocale\fR(3C) is not being called to change the locale. The ! \fBsprintf()\fR and \fBsnprintf()\fR functions are Async-Signal-Safe. ! .sp ! .LP ! See \fBstandards\fR(5) for the standards conformance of \fBprintf()\fR, ! \fBfprintf()\fR, \fBsprintf()\fR, and \fBsnprintf()\fR. The \fBasprintf()\fR ! function is modeled on the one that appears in the FreeBSD, NetBSD, and GNU C ! libraries. ! .SH SEE ALSO ! .sp ! .LP ! \fBexit\fR(2), \fBlseek\fR(2), \fBwrite\fR(2), \fBabort\fR(3C), \fBecvt\fR(3C), ! \fBexit\fR(3C), \fBfclose\fR(3C), \fBfflush\fR(3C), \fBfputwc\fR(3C), ! \fBfree\fR(3C), \fBmalloc\fR(3C), \fBputc\fR(3C), \fBscanf\fR(3C), ! \fBsetlocale\fR(3C), \fBstdio\fR(3C), \fBvprintf\fR(3C), \fBwcstombs\fR(3C), ! \fBwctomb\fR(3C), \fBattributes\fR(5), \fBenviron\fR(5), \fBstandards\fR(5) ! .SH NOTES ! .sp ! .LP ! If the \fBj\fR length modifier is used, 32-bit applications that were compiled ! using \fBc89\fR on releases prior to Solaris 10 will experience undefined ! behavior. ! .sp ! .LP ! The \fBsnprintf()\fR return value when \fIn\fR = 0 was changed in the Solaris ! 10 release. The change was based on the SUSv3 specification. The previous ! behavior was based on the initial SUSv2 specification, where \fBsnprintf()\fR ! when \fIn\fR = 0 returns an unspecified value less than 1. --- 43,1242 ---- .\" Copyright 1989 AT&T .\" Portions Copyright 1999, Forrest J. Cavalier III. All Rights Reserved. .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved. .\" Copyright (c) 2009, Sun Microsystems, Inc. All Rights Reserved. .\" ! .Dd July 10, 2020 ! .Dt PRINTF 3C ! .Os ! .Sh NAME ! .Nm printf , ! .Nm fprintf , ! .Nm sprintf , ! .Nm snprintf , ! .Nm asprintf ! .Nd print formatted output ! .Sh LIBRARY ! .Lb libc ! .Sh SYNOPSIS ! .In stdio.h ! .Ft int ! .Fo printf ! .Fa "const char *restrict format" ! .Fa "/* args */ ..." ! .Fc ! .Ft int ! .Fo fprintf ! .Fa "FILE *restrict stream" ! .Fa "const char *restrict format" ! .Fa "/* args */ ..." ! .Fc ! .Ft int ! .Fo sprintf ! .Fa "char *restrict s" ! .Fa "const char *restrict format" ! .Fa "/* args */ ..." ! .Fc ! .Ft int ! .Fo snprintf ! .Fa "char *restrict s" ! .Fa "size_t n" ! .Fa "const char *restrict format" ! .Fa "/* args */ ..." ! .Fc ! .Ft int ! .Fo asprintf ! .Fa "char **ret" ! .Fa "const char *restrict format" ! .Fa "/* args */ ..." ! .Fc ! .Sh DESCRIPTION ! The ! .Fn printf ! function places output on the standard output stream ! .Dv stdout . ! .Pp ! The ! .Fn fprintf ! function places output on on the named output stream ! .Fa stream . ! .Pp ! The ! .Fn sprintf ! function places output, followed by the null byte ! .Pq Sq \e0 , ! in consecutive bytes starting at ! .Fa s ; ! it is the user's responsibility to ensure that enough storage is available. ! .Pp ! The ! .Fn snprintf ! function is identical to ! .Fn sprintf ! with the addition of the argument ! .Fa n , ! which specifies the size of the buffer referred to by ! .Fa s . ! If ! .Fa n ! is 0, nothing is written and ! .Fa s ! can be a ! .Dv NULL ! pointer. ! Otherwise, output bytes beyond the ! .Fa n Ns -1st ! are discarded instead of being written to the array and a null byte is written ! at the end of the bytes actually written into the array. ! .Pp ! The ! .Fn asprintf ! function is the same as the ! .Fn sprintf ! function except that it returns, in the ! .Fa ret ! argument, a pointer to a buffer sufficiently large to hold the output string. ! This pointer should be passed to ! .Xr free 3C ! to release the allocated storage when it is no longer needed. ! If sufficient space cannot be allocated, the ! .Fn asprintf ! function returns -1 and sets ! .Fa ret ! to be a ! .Dv NULL ! pointer. ! .Pp Each of these functions converts, formats, and prints its arguments under ! control of the ! .Fa format . ! The ! .Fa format ! is a character string, beginning and ending in its initial shift state, if any. ! The ! .Fa format ! is composed of zero or more directives: ordinary characters, which are simply ! copied to the output stream and conversion specifications, each of which results ! in the fetching of zero or more arguments. ! The results are undefined if there are insufficient arguments for the ! .Fa format . ! If the ! .Fa format ! is exhausted while arguments remain, the excess arguments are evaluated but are ! otherwise ignored. ! .Pp ! Conversions can be applied to the ! .Ar n Ns th ! argument after the ! .Fa format ! in the argument list, rather than to the next unused argument. ! In this case, the conversion specifier ! .Cm % ! .Pq see below ! is replaced by the sequence ! .Cm % Ns Ar n Ns Cm $ , ! where ! .Ar n ! is a decimal integer in the range ! .Bq 1, Dv NL_ARGMAX , ! giving the position of the argument in the argument list. This feature provides for the definition of format strings that select ! arguments in an order appropriate to specific languages (see the ! .Sx EXAMPLES section). ! .Pp ! In format strings containing the ! .Cm % Ns Ar n Ns Cm $ ! form of conversion specifications, numbered arguments in the argument list can ! be referenced from the format string as many times as required. ! .Pp ! In format strings containing the ! .Cm % ! form of conversion specifications, each argument in the argument list is used ! exactly once. ! .Pp ! All forms of the ! .Fn printf ! functions allow for the insertion of a language-dependent radix character in the ! output string. ! The radix character is defined by the program's locale ! .Pq category Dv LC_NUMERIC . ! In the POSIX locale, or in a locale where the radix character is not defined, ! the radix character defaults to a period ! .Pq \&. . ! .Ss Conversion Specifications ! Each conversion specification is introduced by the ! .Cm % ! character or by the character sequence ! .Cm % Ns Ar n Ns Cm $ , ! after which the following appear in sequence: ! .Bl -bullet ! .It ! An optional field, consisting of a decimal digit string followed by a ! .Cm $ , ! specifying the next argument to be converted. ! If this field is not provided, the ! .Fa args ! following the last argument converted will be used. ! .It ! Zero or more flags (in any order), which modify the meaning of the conversion specification. ! .It ! An optional minimum field width. ! If the converted value has fewer bytes than the field width, it will be padded ! with spaces by default on the left; it will be padded on the right, if the ! left-adjustment flag ! .Pq Cm - , ! described below, is given to the field width. ! The field width takes the form of an asterisk ! .Pq Cm * , ! described below, or a decimal integer. ! .Pp ! If the conversion specifier is ! .Cm s , ! a standard-conforming application ! .Pq see Xr standards 5 ! interprets the field width as the minimum number of bytes to be printed; an ! application that is not standard-conforming interprets the field width as the minimum number of columns of screen display. ! For an application that is not standard-conforming, ! .Ql %10s ! means if the converted value has a screen width of 7 columns, 3 spaces would be ! padded on the right. ! .Pp ! If the format is ! .Cm %ws , ! then the field width should be interpreted as the minimum number of columns of ! screen display. ! .It ! An optional precision that gives the minimum number of digits to appear for the ! .Cm d , i , o , u , x , ! and ! .Cm X ! conversions (the field is padded with leading zeros); the number of digits to ! appear after the radix character for the ! .Cm a , A , e , E , f , ! and ! .Cm F ! conversions, the maximum number of significant digits for the ! .Cm g ! and ! .Cm G ! conversions; or the maximum number of bytes to be printed from a string in ! .Cm s ! and ! .Cm S ! conversions. ! The precision takes the form of a period ! .Pq Cm \&. ! followed either by an asterisk ! .Pq Cm * , ! described below, or an optional decimal digit string, where a null digit string ! is treated as 0. ! If a precision appears with any other conversion specifier, the behavior is ! undefined. ! .Pp ! If the conversion specifier is ! .Cm s ! or ! .Cm S , ! a standard-conforming application ! .Pq see Xr standards 5 ! interprets the precision as the maximum number of bytes to be written; an ! application that is not standard-conforming interprets the precision as the ! maximum number of columns of screen display. ! For an application that is not standard-conforming, ! .Ql %.5s ! would print only the portion of the string that would display in 5 screen ! columns. ! Only complete characters are written. ! .Pp ! For ! .Cm %ws , ! the precision should be interpreted as the maximum number of columns of screen ! display. ! The precision takes the form of a period ! .Pq Cm \&. followed by a decimal digit string; a null digit string is treated as zero. Padding specified by the precision overrides the padding specified by the field width. ! .It ! An optional length modifier that specified the size of the argument. ! .It ! A conversion specifier that indicates the type of conversion to be applied. ! .El ! .Pp A field width, or precision, or both can be indicated by an asterisk ! .Pq Cm * . ! In this case, an argument of type ! .Vt int ! supplies the field width or precision. ! Arguments specifying field width, or precision, or both must appear in that ! order before the argument, if any, to be converted. ! A negative field width is taken as a ! .Cm - ! flag followed by a positive field width. ! A negative precision is taken as if the precision were omitted. ! In format strings containing the ! .Cm % Ns Ar n Ns Cm $ ! form of a conversion specification, a field width or precision may be indicated ! by the sequence ! .Cm * Ns Ar m Ns Cm $ , ! where ! .Ar m ! is a decimal integer in the range ! .Bq 1, Dv NL_ARGMAX ! giving the position in the argument list (after the format argument) of an ! integer argument containing the field width or precision, for example: ! .Pp ! .Dl printf("%1$d:%2$.*3$d:%4$.*3$d\en", hour, min, precision, sec); ! .Pp ! The ! .Fa format ! can contain either numbered argument specifications (that is, ! .Cm % Ns Ar n Ns Cm $ ! and ! .Cm * Ns Ar m Ns Cm $ ) , ! or unnumbered argument specifications (that is, ! .Cm % ! and ! .Cm * ) , ! but normally not both. ! The only exception to this is that ! .Cm %% ! can be mixed with the ! .Cm % Ns Ar n Ns Cm $ ! form. ! The results of mixing numbered and unnumbered argument specifications in a ! .Fa format ! string are undefined. ! When numbered argument specifications are used, specifying the ! .Ar N Ns th ! argument requires that all the leading arguments, from the first to the ! .Po Ar N Ns -1 Pc Ns th , ! are specified in the format string. ! .Ss Flag Characters The flag characters and their meanings are: ! .Bl -tag -width "' ' (space)" ! .It Cm ' ! The integer portion of the result of a decimal conversion ! .Po Cm %i , %d , %u , %f , %F , %g , ! or ! .Cm %G ! .Pc ! will be formatted with thousands' grouping characters. ! For other conversions the behavior is undefined. ! The non-monetary grouping character is used. ! .It Cm - ! The result of the conversion will be left-justified within the field. ! The conversion will be right-justified if this flag is not specified. ! .It Cm + ! The result of a signed conversion will always begin with a sign ! .Po ! .Cm + ! or ! .Cm - ! .Pc . ! The conversion will begin with a sign only when a negative value is converted if this flag is not specified. ! .It Qo "\ " Qc (space) If the first character of a signed conversion is not a sign or if a signed conversion results in no characters, a space will be placed before the result. ! This means that if the space and ! .Cm + ! flags both appear, the space flag will be ignored. ! .It Cm # ! The value is to be converted to an alternate form. ! For ! .Cm c , d , i , s , ! and ! .Cm u ! conversions, the flag has no effect. ! For an ! .Cm o ! conversion, it increases the precision (if necessary) to force the ! first digit of the result to be a zero. ! For ! .Cm x ! or ! .Cm X ! conversion, a non-zero result will have ! .Ql 0x ! .Pq or Ql 0X ! prepended to it. ! For ! .Cm a , A , e , E , f , F , g , ! and ! .Cm G ! conversions, the result will always contain a radix character, even if no digits ! follow the radix character. ! Without this flag, the radix character appears in the result of these ! conversions only if a digit follows it. ! For ! .Cm g ! and ! .Cm G conversions, trailing zeros will not be removed from the result as they normally are. ! .It Cm 0 ! For ! .Cm d , i , o , u , x , X , a , A , e , E , f , F , g , ! and ! .Cm G ! conversions, leading zeros (following any indication of sign or base) are used ! to pad to the field width; no space padding is performed. ! If the ! .Cm 0 ! and ! .Cm \- ! flags both appear, the ! .Cm 0 ! flag will be ignored. ! For ! .Cm d , i , o , u , x , ! and ! .Cm X ! conversions, if a precision is specified, the ! .Cm 0 ! flag will be ignored. ! If the ! .Cm 0 ! and ! .Cm ' ! flags both appear, the grouping characters are inserted before zero padding. ! For other conversions, the behavior is undefined. ! .El ! .Ss Length Modifiers The length modifiers and their meanings are: ! .Bl -tag -width "ll (ell-ell)" ! .It Cm hh ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt signed char ! or ! .Vt unsigned char argument (the argument will have been promoted according to the integer ! promotions, but its value will be converted to ! .Vt signed char ! or ! .Vt unsigned char ! before printing); or that a following ! .Cm n ! conversion specifier applies to a pointer to a ! .Vt signed char ! argument. ! .It Cm h ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt short ! or ! .Vt unsigned short ! argument (the argument will have been promoted according to the integer ! promotions, but its value will be converted to ! .Vt short ! or ! .Vt unsigned short ! before printing); or that a following ! .Cm n ! conversion specifier applies to a pointer to a ! .Vt short ! argument. ! .It Cm l No (ell) ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt long ! or ! .Vt unsigned long ! argument; that a following ! .Cm n ! conversion specifier applies to a pointer to a ! .Vt long ! argument; that a following ! .Cm c ! conversion specifier applies to a ! .Vt wint_t ! argument; that a following ! .Cm s ! conversion specifier applies to a pointer to a ! .Vt wchar_t ! argument; or has no effect on a following ! .Cm a , A , e , E , f , F , g , ! or ! .Cm G conversion specifier. ! .It Cm ll No (ell-ell) ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt long long ! or ! .Vt unsigned long long ! argument; or that a following ! .Cm n ! conversion specifier applies to a pointer to a ! .Vt long long argument. ! .It Cm j ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to an ! .Vt intmax_t ! or ! .Vt uintmax_t ! argument; or that a following ! .Cm n ! conversion specifier applies to a pointer to an ! .Vt intmax_t ! argument. ! See ! .Sx NOTES . ! .It Cm z ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt size_t ! or the corresponding signed integer type argument; or that a following ! .Cm n ! conversion specifier applies to a pointer to a signed integer type corresponding ! to ! .Vt size_t ! argument. ! .It Cm t ! Specifies that a following ! .Cm d , i , o , u , x , ! or ! .Cm X ! conversion specifier applies to a ! .Vt ptrdiff_t ! or the corresponding unsigned type argument; or that a following ! .Cm n ! conversion specifier applies to a pointer to a ! .Vt ptrdiff_t ! argument. ! .It Cm L ! Specifies that a following ! .Cm a , A , e , E , f , F , g , ! or ! .Cm G ! conversion specifier applies to a ! .Vt long double ! argument. ! .El ! .Pp If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined. ! .Ss Conversion Specifiers ! Each conversion specifier results in fetching zero or more arguments. ! The results are undefined if there are insufficient arguments for the format. ! If the format is exhausted while arguments remain, the excess arguments are ignored. ! .Pp The conversion specifiers and their meanings are: ! .Bl -tag -width "d, i" ! .It Cm d , i ! The ! .Vt int ! argument is converted to a signed decimal in the style ! .Ql [-]dddd . ! The precision specifies the minimum number of digits to appear; if the value ! being converted can be represented in fewer digits, it will be expanded with ! leading zeros. ! The default precision is 1. ! The result of converting 0 with an explicit precision of 0 is no characters. ! .It Cm o ! The ! .Vt unsigned int ! argument is converted to unsigned octal format in the style ! .Ql dddd . ! The precision specifies the minimum number of digits to appear; if the value ! being converted can be represented in fewer digits, it will be expanded with ! leading zeros. ! The default precision is 1. ! The result of converting 0 with an explicit precision of 0 is no characters. ! .It Cm u ! The ! .Vt unsigned int ! argument is converted to unsigned decimal format in the style ! .Ql dddd . ! The precision specifies the minimum number of digits to appear; if the value ! being converted can be represented in fewer digits, it will be expanded with ! leading zeros. ! The default precision is 1. ! The result of converting 0 with an explicit precision of 0 is no characters. ! .It Cm x ! The ! .Vt unsigned int ! argument is converted to unsigned hexadecimal format in the style ! .Ql dddd ; ! the letters ! .Ql abcdef ! are used. ! The precision specifies the minimum number of digits to appear; if the value ! being converted can be represented in fewer digits, it will be expanded with ! leading zeros. ! The default precision is 1. ! The result of converting 0 with an explicit precision of 0 is no characters. ! .It Cm X ! Behaves the same as the ! .Cm x ! conversion specifier except that letters ! .Ql ABCDEF ! are used instead of ! .Ql abcdef . ! .It Cm f , F ! The ! .Vt double ! argument is converted to decimal notation in the style ! .Ql [-]ddd.ddd , ! where the number of digits after the radix character ! .Pq see Xr setlocale 3C ! is equal to the precision specification. ! If the precision is missing it is taken as 6; if the precision is explicitly 0 ! and the ! .Cm # ! flag is not specified, no radix character appears. ! If a radix character appears, at least 1 digit appears before it. ! The converted value is rounded to fit the specified output format according to ! the prevailing floating point rounding direction mode. ! If the conversion is not exact, an inexact exception is raised. ! .Pp ! For the ! .Cm f ! specifier, a ! .Vt double ! argument representing an infinity or NaN is converted in the style of the ! .Cm e ! conversion specifier, except that for an infinite argument, ! .Ql infinity ! or ! .Ql Infinity ! is printed when the precision is at least 8 and ! .Ql inf ! or ! .Ql Inf ! is printed otherwise. ! .Pp ! For the ! .Cm F ! specifier, a ! .Vt double ! argument representing an infinity or NaN is converted in the SUSv3 style of the ! .Cm E ! conversion specifier, except that for an infinite argument, ! .Ql INFINITY ! is printed when the precision is at least 8 and ! .Ql INF ! is printed otherwise. ! .It Cm e , E ! The ! .Vt double ! argument is converted to the style ! .Ql [-]d.ddde+-dd , ! where there is one digit before the radix character (which is non-zero if the ! argument is non-zero) and the number of digits after it is equal to the ! precision. ! When the precision is missing it is taken as 6; if the precision is 0 and the ! .Cm # ! flag is not specified, no radix character appears. ! The ! .Cm E ! conversion specifier will produce a number with ! .Ql E ! instead of ! .Ql e ! introducing the exponent. ! The exponent always contains at least two digits. ! The converted value is rounded to fit the specified output format according to ! the prevailing floating point rounding direction mode. ! If the conversion is not exact, an inexact exception is raised. ! .Pp Infinity and NaN values are handled in one of the following ways: ! .Bl -tag -width "Default" ! .It SUSv3 ! For the ! .Cm e ! specifier, a ! .Vt double ! argument representing an infinity is printed as ! .Ql [-]infinity , ! when the precision for the conversion is at least 7 and as ! .Ql [-]inf ! otherwise. ! A ! .Vt double ! argument representing a NaN is printed as ! .Ql [-]nan . ! For the ! .Cm E ! specifier, ! .Ql INF , ! .Ql INFINITY , ! and ! .Ql NAN ! are printed instead of ! .Ql inf , ! .Ql infinity , ! and ! .Ql nan , ! respectively. ! Printing of the sign follows the rules described above. ! .It Default ! A ! .Vt double ! argument representing an infinity is printed as ! .Ql [-]Infinity , ! when the precision for the conversion is at least 7 and as ! .Ql [-]Inf ! otherwise. ! A ! .Vt double ! argument representing a NaN is printed as ! .Ql [-]NaN . ! Printing of the sign follows the rules described above. ! .El ! .It Cm g , G ! The ! .Vt double ! argument is printed in style ! .Cm f ! or ! .Cm e ! (or in style ! .Cm E ! in the case of a ! .Cm G ! conversion specifier), with the precision specifying the number of significant ! digits. ! If an explicit precision is 0, it is taken as 1. ! The style used depends on the value converted: style ! .Cm e ! .Pq or Cm E ! will be used only if the exponent resulting from the conversion is less than -4 ! or greater than or equal to the precision. ! Trailing zeros are removed from the fractional part of the result. ! A radix character appears only if it is followed by a digit. ! .Pp ! A ! .Vt double ! argument representing an infinity or NaN is converted in the style of the ! .Cm e ! or ! .Cm E ! conversion specifier, except that for an infinite argument, ! .Ql infinity , ! .Ql INFINITY , ! or ! .Ql Infinity ! is printed when the precision is at least 8 and ! .Ql inf , ! .Ql INF , ! or ! .Ql Inf ! is printed otherwise. ! .It Cm a , A ! A ! .Vt double ! argument representing a floating-point number is converted in the style ! .Ql [-]0xh.hhhhp+-d , ! where the single hexadecimal digit preceding the radix point is 0 if the value ! converted is zero and 1 otherwise and the number of hexadecimal digits after it ! is equal to the precision; if the precision is missing, the number of digits ! printed after the radix point is 13 for the conversion of a ! .Vt double ! value, 16 for the conversion of a ! .Vt long double ! value on x86, and 28 for the conversion of a ! .Vt long double ! value on SPARC; if the precision is zero and the ! .Cm # ! flag is not specified, no decimal-point character will appear. ! The letters ! .Ql abcdef ! are used for ! .Cm a ! conversion and the letters ! .Ql ABCDEF ! for ! .Cm A ! conversion. ! The ! .Cm A ! conversion specifier produces a number with ! .Ql X ! and ! .Ql P ! instead of ! .Ql x ! and ! .Ql p . ! The exponent will always contain at least one digit, and only as many more ! digits as necessary to represent the decimal exponent of 2. ! If the value is zero, the exponent is zero. ! .Pp The converted value is rounded to fit the specified output format according to ! the prevailing floating point rounding direction mode. ! If the conversion is not exact, an inexact exception is raised. ! .Pp ! A ! .Vt double ! argument representing an infinity or NaN is converted in the SUSv3 style of an ! .Cm e ! or ! .Cm E ! conversion specifier. ! .It Cm c ! The ! .Vt int ! argument is converted to an ! .Vt unsigned char , ! and the resulting byte is printed. ! .Pp ! If an ! .Cm l No (ell) ! qualifier is present, the ! .Vt wint_t ! argument is converted as if by an ! .Cm ls ! conversion specification with no precision and an argument that points to a ! two-element array of type ! .Vt wchar_t , ! the first element of which contains the ! .Vt wint_t ! argument to the ! .Cm ls ! conversion specification and the second element contains a null wide-character. ! .It Cm C ! Same as ! .Cm lc . ! .It Cm wc ! The ! .Vt int ! argument is converted to a wide character ! .Pq Vt wchar_t , ! and the resulting wide character is printed. ! .It Cm s ! The argument must be a pointer to an array of ! .Vt char . ! Bytes from the array are written up to (but not including) any terminating null ! byte. ! If a precision is specified, a standard-conforming application ! .Pq see Xr standards 5 ! will write only the number of bytes specified by precision; an application that ! is not standard-conforming will write only the portion of the string that will ! display in the number of columns of screen display specified by precision. ! If the precision is not specified, it is taken to be infinite, so all bytes up ! to the first null byte are printed. ! An argument with a null value will yield undefined results. ! .Pp ! If an ! .Cm l No (ell) ! qualifier is present, the argument must be a pointer to an array of type ! .Vt wchar_t . ! Wide-characters from the array are converted to characters (each as if by a call ! to the ! .Xr wcrtomb 3C ! function, with the conversion state described by an ! .Vt mbstate_t ! object initialized to zero before the first wide-character is converted) up to ! and including a terminating null wide-character. ! The resulting characters are written up to (but not including) the terminating ! null character (byte). ! If no precision is specified, the array must contain a null wide-character. ! If a precision is specified, no more than that many characters (bytes) are ! written (including shift sequences, if any), and the array must contain a null ! wide-character if, to equal the character sequence length given by the ! precision, the function would need to access a wide-character one past the end ! of the array. ! In no case is a partial character written. ! .It Cm S ! Same as ! .Cm ls . ! .It Cm ws ! The argument must be a pointer to an array of ! .Vt wchar_t . ! Bytes from the array are written up to (but not including) any terminating null ! character. ! If the precision is specified, only that portion of the wide-character array ! that will display in the number of columns of screen display specified by ! precision will be written. ! If the precision is not specified, it is taken to be infinite, so all wide ! characters up to the first null character are printed. ! An argument with a null value will yield undefined results. ! .It Cm p ! The argument must be a pointer to ! .Vt void . ! The value of the pointer is converted to a set of sequences of printable ! characters, which should be the same as the set of sequences that are matched by ! the ! .Cm %p ! conversion of the ! .Xr scanf 3C ! function. ! .It Cm n The argument must be a pointer to an integer into which is written the number of bytes written to the output standard I/O stream so far by this call to one ! of the ! .Fn printf ! functions. ! No argument is converted. ! .It Cm % ! Print a ! .Ql % ; ! no argument is converted. ! The entire conversion specification must be ! .Cm %% . ! .El ! .Pp If a conversion specification does not match one of the above forms, the behavior is undefined. ! .Pp In no case does a non-existent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field ! is simply expanded to contain the conversion result. ! Characters generated by ! .Fn printf ! and ! .Fn fprintf ! are printed as if the ! .Xr putc 3C function had been called. ! .Pp ! The ! .Va st_ctime ! and ! .Va st_mtime ! fields of the file will be marked for update between the call to a successful ! execution of ! .Fn printf ! or ! .Fn fprintf ! and the next successful completion of a call to ! .Xr fflush 3C ! or ! .Xr fclose 3C ! on the same stream or a call to ! .Xr exit 3C ! or ! .Xr abort 3C . ! .Sh RETURN VALUES ! The ! .Fn printf , ! .Fn fprintf , ! .Fn sprintf , ! and ! .Fn asprintf functions return the number of bytes transmitted (excluding the terminating ! null byte in the case of ! .Fn sprintf ! and ! .Fn asprintf ) . ! .Pp ! The ! .Fn snprintf ! function returns the number of bytes that would have been written to ! .Fa s ! if ! .Fa n ! had been sufficiently large (excluding the terminating null byte). ! If the value of ! .Fa n ! is 0 on a call to ! .Fn snprintf , ! .Fa s ! can be a null pointer and the number of bytes that would have been written if ! .Fa n ! had been sufficiently large (excluding the terminating null byte) is returned. ! .Pp Each function returns a negative value if an output error was encountered. ! .Sh USAGE ! If the application calling the ! .Fn printf ! functions has any objects of type ! .Vt wint_t ! or ! .Vt wchar_t , ! it must also include the header ! .In wchar.h to have these objects defined. ! .Ss Escape Character Sequences It is common to use the following escape sequences built into the C language ! when entering format strings for the ! .Fn printf ! functions, but these sequences are processed by the C compiler, not by the ! .Fn printf ! function. ! .Bl -tag -width "\ea" ! .It \ea ! Alert. ! Ring the bell. ! .It \eb ! Backspace. ! Move the printing position to one character before the current position, unless ! the current position is the start of a line. ! .It \ef ! Form feed. ! Move the printing position to the initial printing position of the next logical ! page. ! .It \en ! Newline. ! Move the printing position to the start of the next line. ! .It \er ! Carriage return. ! Move the printing position to the start of the current line. ! .It \et ! Horizontal tab. ! Move the printing position to the next implementation-defined horizontal tab ! position on the current line. ! .It \ev ! Vertical tab. ! Move the printing position to the start of the next implementation-defined ! vertical tab position. ! .El ! .Pp In addition, the C language supports character sequences of the form ! .Cm \e Ns Ar octal-number and ! .Cm \e Ns Ar hex-number which translates into the character represented by the octal or hexadecimal ! number. ! For example, if ASCII representations are being used, the letter 'a' may be ! written as ! .Ql \e141 ! and 'Z' as ! .Ql \e132 . ! This syntax is most frequently used to represent the null character as ! .Ql \e0 . ! This is exactly equivalent to the numeric constant zero (0). ! Note that the octal number does not include the zero prefix as it would for a ! normal octal constant. ! To specify a hexadecimal number, omit the zero so that the prefix is an 'x' ! (uppercase 'X' is not allowed in this context). ! Support for hexadecimal sequences is an ANSI extension. ! See ! .Xr standards 5 . ! .Sh EXAMPLES ! .Sy Example 1 ! To print the language-independent date and time format, the following statement could be used: ! .Pp ! .Dl printf (format, weekday, month, day, hour, min); ! .Pp ! For American usage, ! .Fa format ! could be a pointer to the string: ! .Pp ! .Dl Qq %s, %s %d, %d:%.2d\en ! .Pp producing the message: ! .Pp ! .Dl Sunday, July 3, 10:02 ! .Pp ! whereas for German usage, ! .Fa format ! could be a pointer to the string: ! .Pp ! .Dl Qq %1$s, %3$d. %2$s, %4$d:%5$.2d\en ! .Pp producing the message: ! .Pp ! .Dl Sonntag, 3. Juli, 10:02 ! .Pp ! .Sy Example 2 ! To print a date and time in the form ! .Ql Sunday, July 3, 10:02 , ! where ! .Va weekday ! and ! .Va month ! are pointers to null-terminated strings: ! .Pp ! .Dl printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min); ! .Pp ! .Sy Example 3 ! To print pi to 5 decimal places: ! .Pp ! .Dl printf("pi = %.5f", 4 * atan(1.0)); ! .Pp ! .Sy Example 4 ! The following example applies only to applications that are not ! standard-conforming. ! To print a list of names in columns which are 20 characters wide: ! .Pp ! .Dl printf("%20s%20s%20s", lastname, firstname, middlename); ! .Sh ERRORS ! For the conditions under which ! .Fn printf ! and ! .Fn fprintf ! will fail and may fail, refer to ! .Xr fputc 3C ! or ! .Xr fputwc 3C . ! .Pp ! The ! .Fn snprintf ! function will fail if: ! .Bl -tag -width Er ! .It Er EOVERFLOW ! The value of ! .Fa n ! is greater than ! .Dv INT_MAX ! or the number of bytes needed to hold the output excluding the terminating null ! is greater than ! .Dv INT_MAX . ! .El ! .Pp ! The ! .Fn printf , ! .Fn fprintf , ! .Fn sprintf , ! and ! .Fn snprintf ! functions may fail if: ! .Bl -tag -width Er ! .It Er EILSEQ ! A wide-character code that does not correspond to a valid character has been ! detected. ! .It Er EINVAL ! There are insufficient arguments. ! .El ! .Pp ! The ! .Fn printf , ! .Fn fprintf , ! and ! .Fn asprintf ! functions may fail due to an underlying ! .Xr malloc 3C ! failure if: ! .Bl -tag -width Er ! .It Er EAGAIN ! Storage space is temporarily unavailable. ! .It Er ENOMEM ! Insufficient storage space is available. ! .El ! .Sh CODE SET INDEPENDENCE ! .Sy Enabled ! .Sh INTERFACE STABILITY ! .Sy Committed ! .Sh MT-LEVEL All of these functions can be used safely in multithreaded applications, as ! long as ! .Xr setlocale 3C ! is not being called to change the locale. ! The ! .Fn sprintf ! and ! .Fn snprintf ! functions are ! .Sy Async-Signal-Safe . ! .Sh SEE ALSO ! .Xr exit 2 , ! .Xr lseek 2 , ! .Xr write 2 , ! .Xr abort 3C , ! .Xr ecvt 3C , ! .Xr exit 3C , ! .Xr fclose 3C , ! .Xr fflush 3C , ! .Xr fputwc 3C , ! .Xr free 3C , ! .Xr malloc 3C , ! .Xr putc 3C , ! .Xr scanf 3C , ! .Xr setlocale 3C , ! .Xr stdio 3C , ! .Xr vprintf 3C , ! .Xr wcstombs 3C , ! .Xr wctomb 3C , ! .Xr attributes 5 , ! .Xr environ 5 , ! .Xr standards 5 ! .Sh STANDARDS ! See ! .Xr standards 5 ! for the standards conformance of ! .Fn printf , ! .Fn fprintf , ! .Fn sprintf , ! and ! .Fn snprintf . ! The ! .Fn asprintf ! function is modeled on the one that appears in the ! .Fx , ! .Nx , ! and GNU C libraries. ! .Sh NOTES ! If the ! .Cm j ! length modifier is used, 32-bit applications that were compiled using ! .Nm c89 ! on releases prior to Solaris 10 will experience undefined behavior. ! .Pp ! The ! .Fn snprintf ! return value when ! .Fa n ! is 0 was changed in the Solaris 10 release. ! The change was based on the SUSv3 specification. ! The previous behavior was based on the initial SUSv2 specification, where ! .Fn snprintf ! when ! .Fa n ! is 0 returns an unspecified value less than 1.