Standard I/O Functions printf(3S) NAME printf, fprintf, sprintf, snprintf - print formatted output SYNOPSIS #include int printf(const char *format, /* args*/ ...); int fprintf(FILE *stream, const char *format, /* args*/ ...); int sprintf(char *s, const char *format, /* args*/ ...); int snprintf(char *s, size_t n, const char *format, /* args*/ ...); DESCRIPTION The printf() function places output on the standard output stream stdout. The fprintf() function places output on on the named output stream _s_t_r_e_a_m. The sprintf() function places output, followed by the null byte (\0), in consecutive bytes starting at _s; it is the user's responsibility to ensure that enough storage is available. The snprintf() function is identical to sprintf() with the addition of the argument _n, which specifies the size of the buffer referred to by _s. The buffer is terminated with the null byte only if space is available. Each of these functions converts, formats, and prints its _a_r_g_s under control of the _f_o_r_m_a_t. The _f_o_r_m_a_t is a character string, beginning and ending in its initial shift state, if any. The _f_o_r_m_a_t is composed of zero or more directives defined as follows: +o _o_r_d_i_n_a_r_y _c_h_a_r_a_c_t_e_r_s, which are simply copied to the output stream; +o _e_s_c_a_p_e _s_e_q_u_e_n_c_e_s, which represent non-graphic charac- ters; and +o _c_o_n_v_e_r_s_i_o_n _s_p_e_c_i_f_i_c_a_t_i_o_n_s, each of which results in the fetching of zero or more arguments. The results are undefined if there are insufficient argu- ments for the _f_o_r_m_a_t. If the _f_o_r_m_a_t is exhausted while argu- ments remain, the excess arguments are evaluated but are otherwise ignored. SunOS 5.7 Last change: 7 Oct 1998 1 Standard I/O Functions printf(3S) Conversions can be applied to the _nth argument after the _f_o_r_m_a_t in the argument list, rather than to the next unused argument. In this case, the conversion character % (see below) is replaced by the sequence %_n$, where _n is a decimal integer in the range [1, 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 EXAMPLES section). In format strings containing the %_n$ form of conversion specifications, numbered arguments in the argument list can be referenced from the format string as many times as required. In format strings containing the % form of conversion specifications, each argument in the argument list is used exactly once. All forms of the printf() functions allow for the insertion of a language-dependent radix character. The radix character is defined by the program's locale (category LC_NUMERIC). In the POSIX locale, or in a locale where the radix character is not defined, the radix character defaults to a period (.). Escape Sequences The following escape sequences produce the associated action on display devices capable of the action: \a Alert. Ring the bell. \b Backspace. Move the printing position to one char- acter before the current position, unless the current position is the start of a line. \f Form feed. Move the printing position to the ini- tial printing position of the next logical page. \n Newline. Move the printing position to the start of the next line. \r Carriage return. Move the printing position to the start of the current line. \t Horizontal tab. Move the printing position to the next implementation-defined horizontal tab posi- tion on the current line. \v Vertical tab. Move the printing position to the start of the next implementation-defined vertical tab position. SunOS 5.7 Last change: 7 Oct 1998 2 Standard I/O Functions printf(3S) Conversion Specifications Each conversion specification is introduced by the character % or by the character sequence %_n$, after which the follow- ing appear in sequence: +o An optional field, consisting of a decimal digit string followed by a $, specifying the next _a_r_g_s to be converted. If this field is not provided, the _a_r_g_s following the last _a_r_g_s converted will be used. +o Zero or more _f_l_a_g_s (in any order), which modify the meaning of the conversion specification. +o An optional minimum _f_i_e_l_d _w_i_d_t_h. 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 (-), described below, is given to the field width. The field width takes the form of an asterisk (*), described below, or a decimal integer. If the conversion character is s, a standard- conforming application (see standards(5)) inter- prets 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, %10s means if the converted value has a screen width of 7 columns, 3 spaces would be padded on the right. If the format is %ws, then the field width should be interpreted as the minimum number of columns of screen display. +o An optional _p_r_e_c_i_s_i_o_n that gives the minimum number of digits to appear for the d, i, o, u, x, or X conver- sions (the field is padded with leading zeros); the number of digits to appear after the radix character for the e, E, and f conversions, the maximum number of significant digits for the g and G conversions; or the maximum number of bytes to be printed from a string in s and S 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 pre- cision appears with any other conversion character, the behavior is undefined. If the conversion character is s or S, a standard-conforming application (see standards(5)) SunOS 5.7 Last change: 7 Oct 1998 3 Standard I/O Functions printf(3S) 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, %.5s would print only the portion of the string that would display in 5 screen columns. Only complete characters are written. For %ws, the precision should be interpreted as the maximum number of columns of screen display. The precision takes the form of a period (.) fol- lowed 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. +o An optional h specifies that a following d, i, o, u, x, or X conversion character applies to a type short int or unsigned short int argument (the argument will be promoted according to the integral promotions and its value converted to type short int or unsigned short int before printing); an optional h specifies that a following n conversion character applies to a pointer to a type short int argument. An optional l (ell) specifies that a following d, i, o, u, x, or X conversion character applies to a type long int or unsigned long int argument; an optional l (ell) specifies that a following n conversion character applies to a pointer to a type long int argument. An optional ll (ell ell) specifies that a following d, i, o, u, x, or X conversion character applies to a type long long or unsigned long long argument; an optional ll (ell ell) specifies that a following n conversion character applies to a pointer to a long long argument. An optional L specifies that a follow- ing e, E, f, g, or G conversion character applies to a long double argument. If an h, l, or L appears before any other conversion character, the behavior is unde- fined. +o An optional l (ell) specifying that a following c conversion character applies to a wint_t argument; an optional l (ell) specifying that a following s conver- sion character applies to a pointer to a wchar_t argument. +o A _c_o_n_v_e_r_s_i_o_n _c_h_a_r_a_c_t_e_r (see below) that indicates the type of conversion to be applied. A field width or precision may be indicated by an asterisk (*) instead of a digit string. In this case, an integer _a_r_g_s SunOS 5.7 Last change: 7 Oct 1998 4 Standard I/O Functions printf(3S) supplies the field width or precision. The _a_r_g_s that is actually converted is not fetched until the conversion letter is seen, so the _a_r_g_s specifying field width or preci- sion must appear before the _a_r_g_s (if any) to be converted. If the _p_r_e_c_i_s_i_o_n argument is negative, it will be changed to zero. A negative field width argument is taken as a - flag, followed by a positive field width. A negative precision is taken as if the precision were omitted. In format strings containing the %_n$ form of a conversion specification, a field width or precision may be indicated by the sequence *_m$, where _m is a decimal integer in the range [1, 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: printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec); The _f_o_r_m_a_t can contain either numbered argument specifica- tions (that is, %_n$ and *_m$), or unnumbered argument specif- ications (that is, % and *), but normally not both. The only exception to this is that %% can be mixed with the %_n$ form. The results of mixing numbered and unnumbered argument specifications in a _f_o_r_m_a_t string are undefined. When num- bered argument specifications are used, specifying the _Nth argument requires that all the leading arguments, from the first to the (_N-_1)th, are specified in the format string. Flag Characters The flag characters and their meanings are: ' The integer portion of the result of a decimal conversion (%i, %d, %u, %f, %g, or %G) will be formatted with thousands' grouping characters. For other conversions the behavior is undefined. The non-monetary grouping character is used. - The result of the conversion will be left- justified within the field. (It will be right- justified if this flag is not specified.) + The result of a signed conversion will always begin with a sign (+ or -). (It will begin with a sign only when a negative value is converted if this flag is not specified.) space If the first character of a signed conversion is not a sign, a space will be placed before the result. This means that if the space and + flags both appear, the space flag will be ignored. # The value is to be converted to an alternate form. For c, d, i, s, and u conversions, the flag has no SunOS 5.7 Last change: 7 Oct 1998 5 Standard I/O Functions printf(3S) effect. For an o conversion, it increases the pre- cision to force the first digit of the result to be a zero. For x (or X) conversion, a non-zero result will have 0x (or 0X) prepended to it. For e, E, f, g, and G conversions, the result will always contain a radix character, even if no digits follow the point (normally, a decimal point appears in the result of these conversions only if a digit follows it). For g and G conversions, trailing zeros will not be removed from the result as they normally are. 0 For d, i, o, u, x, X, e, E, f, g, and G conver- sions, leading zeros (following any indication of sign or base) are used to pad to the field width; no space padding is performed. If the 0 and - flags both appear, the 0 flag will be ignored. For d, i, o, u, x, and X conversions, if a precision is specified, the 0 flag will be ignored. For other conversions, the behavior is undefined. Conversion Characters Each conversion character results in fetching zero or more _a_r_g_s. The results are undefined if there are insufficient _a_r_g_s for the format. If the format is exhausted while _a_r_g_s remain, the excess _a_r_g_s are ignored. The conversion characters and their meanings are: d,i The int argument is converted to a signed decimal in the style [-]_d_d_d_d. 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 convert- ing 0 with an explicit precision of 0 is no char- acters. o The unsigned int argument is converted to unsigned octal format in the style _d_d_d_d. 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. u The unsigned int argument is converted to unsigned decimal format in the style _d_d_d_d. 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 SunOS 5.7 Last change: 7 Oct 1998 6 Standard I/O Functions printf(3S) zeros. The default precision is 1. The result of converting 0 with an explicit precision of 0 is no characters. x The unsigned int argument is converted to unsigned hexadecimal format in the style _d_d_d_d; the letters 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 convert- ing 0 with an explicit precision of 0 is no char- acters. X Behaves the same as the x conversion character except that letters ABCDEF are used instead of abcdef. f The double _a_r_g_s is converted to decimal notation in the style [-]_d_d_d._d_d_d, where the number of digits after the radix character (see setlocale(3C)) is equal to the precision specifi- cation. If the precision is omitted from _a_r_g, six digits are output; if the precision is explicitly zero and the # flag is not specified, no radix character appears. If a radix character appears, at least 1 digit appears before it. The value is rounded to the appropriate number of digits. e,E The double _a_r_g_s is converted to the style [-]_d._d_d_de+__d_d, 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, six digits are produced; if the precision is zero and the # flag is not specified, no radix character appears. The E conversion character will produce a number with E instead of e intro- ducing the exponent. The exponent always contains at least two digits. The value is rounded to the appropriate number of digits. g,G The double _a_r_g_s is printed in style f or e (or in style E in the case of a G conversion character), with the precision specifying the number of signi- ficant digits. If the precision is zero, it is taken as one. The style used depends on the value converted: style e (or 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 SunOS 5.7 Last change: 7 Oct 1998 7 Standard I/O Functions printf(3S) if it is followed by a digit. c The int _a_r_g_s is converted to an unsigned char, and the resulting byte is printed. If an l (ell) qualifier is present, the wint_t argument is converted as if by an ls conversion specification with no precision and an argument that points to a two-element array of type wchar_t, the first element of which contains the wint_t argument to the ls conversion specification and the second element contains a null wide- character. C Same as lc. wc The int _a_r_g_s is converted to a wide character (wchar_t), and the resulting wide character is printed. s The _a_r_g_s is taken to be a string (character pointer) and characters from the string are writ- ten up to (but not including) a terminating null byte. If a precision is specified, a standard- conforming application (see standards(5)) will write only the number of bytes specified by preci- sion; 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. A null value for _a_r_g_s will yield undefined results. If an l (ell) qualifier is present, the argument must be a pointer to an array of type wchar_t. Wide-characters from the array are converted to characters (each as if by a call to the wcrtomb(3C) function, with the conversion state described by an 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 SunOS 5.7 Last change: 7 Oct 1998 8 Standard I/O Functions printf(3S) sequence length given by the precision, the func- tion would need to access a wide-character one past the end of the array. In no case is a par- tial character written. S Same as _l_s. ws The _a_r_g_s is taken to be a wide character string (wide character pointer) and wide characters from the string are written up to (but not including) a terminating null character; if the precision is specified, only the portion of the wide character string 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. A null value for _a_r_g_s will yield undefined results. p The _a_r_g_s should be a pointer to 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 %p conversion of the scanf(3S) function. n The argument should 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 printf(), fprintf(), or sprintf(). No argument is converted. % Print a %; no argument is converted. The entire conversion specification must be %%. If a conversion specification does not match one of the above forms, the behavior is undefined. If a floating-point value is the internal representation for infinity, the output is [+_]_I_n_f_i_n_i_t_y, where _I_n_f_i_n_i_t_y is either Infinity or Inf, depending on the desired output string length. Printing of the sign follows the rules described above. If a floating-point value is the internal representation for "not-a-number," the output is [+_]_N_a_N. Printing of the sign follows the rules described above. 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 printf() and fprintf() are printed as if the putc(3S) SunOS 5.7 Last change: 7 Oct 1998 9 Standard I/O Functions printf(3S) function had been called. The st_ctime and st_mtime fields of the file will be marked for update between the call to a successful execution of printf() or fprintf() and the next successful completion of a call to fflush(3S) or fclose(3S) on the same stream or a call to exit(3C) or abort(3C). RETURN VALUES The printf(), fprintf(), and sprintf() functions return the number of bytes transmitted (not including the \0 in the case of sprintf()). The snprintf() function returns the number of characters formatted, that is, the number of char- acters that would have been written to the buffer if it were large enough. Each function returns a negative value if an output error was encountered. ERRORS For the conditions under which printf() and fprintf() will fail and may fail, refer to fputc(3S) or fputwc(3S). In addition, all forms of printf() may fail if: EILSEQ A wide-character code that does not correspond to a valid character has been detected. EINVAL There are insufficient arguments. In addition, printf() and fprintf() may fail if: ENOMEM Insufficient storage space is available. USAGE If the application calling the printf() functions has any objects of type wint_t or wchar_t, it must also include the header to have these objects defined. The sprintf() and snprintf() functions are MT-Safe in mul- tithreaded applications. The printf() and fprintf() func- tions can be used safely in multithreaded applications, as long as setlocale(3C) is not being called to change the locale. EXAMPLES Example 1: To print the language-independent date and time format, the following statement could be used: printf (format, weekday, month, day, hour, min); For American usage, _f_o_r_m_a_t could be a pointer to the string: SunOS 5.7 Last change: 7 Oct 1998 10 Standard I/O Functions printf(3S) "%s, %s %d, %d:%.2d\n" producing the message: Sunday, July 3, 10:02 whereas for German usage, _f_o_r_m_a_t could be a pointer to the string: "%1$s, %3$d. %2$s, %4$d:%5$.2d\n" producing the message: Sonntag, 3. Juli, 10:02 Example 2: To print a date and time in the form Sunday, July 3, 10:02, where weekday and month are pointers to null- terminated strings: printf("%s, %s %i, %d:%.2d", weekday, month, day, hour, min); Example 3: To print pi to 5 decimal places: printf("pi = %.5f", 4 * atan(1.0)); Default Example 4: The following example applies only to applica- tions which are not standard-conforming (see standards(5)). To print a list of names in columns which are 20 characters wide: printf("%20s%20s%20s", lastname, firstname, middlename); ATTRIBUTES See attributes(5) for descriptions of the following attri- butes: ____________________________________________________________ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | | MT-Level | MT-Safe with exceptions | | CSI | Enabled | |______________________________|______________________________| SEE ALSO exit(2), lseek(2), write(2), abort(3C), ecvt(3C), exit(3C), fclose(3S), fflush(3S), fputwc(3S), putc(3S), scanf(3S), setlocale(3C), stdio(3S), wcstombs(3C), wctomb(3C), attri- butes(5), environ(5), standards(5) SunOS 5.7 Last change: 7 Oct 1998 11