FPRINTF(3)                 POSIX Programmer's Manual                FPRINTF(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       dprintf, fprintf, printf, snprintf, sprintf -- print formatted output

SSYYNNOOPPSSIISS
       #include <stdio.h>

       int dprintf(int _f_i_l_d_e_s, const char *restrict _f_o_r_m_a_t, ...);
       int fprintf(FILE *restrict _s_t_r_e_a_m, const char *restrict _f_o_r_m_a_t, ...);
       int printf(const char *restrict _f_o_r_m_a_t, ...);
       int snprintf(char *restrict _s, size_t _n,
           const char *restrict _f_o_r_m_a_t, ...);
       int sprintf(char *restrict _s, const char *restrict _f_o_r_m_a_t, ...);

DDEESSCCRRIIPPTTIIOONN
       Excluding _d_p_r_i_n_t_f(): The functionality described on this reference page
       is aligned with the ISO C standard. Any conflict between the
       requirements described here and the ISO C standard is unintentional.
       This volume of POSIX.1-2017 defers to the ISO C standard.

       The _f_p_r_i_n_t_f() function shall place output on the named output _s_t_r_e_a_m.
       The _p_r_i_n_t_f() function shall place output on the standard output stream
       _s_t_d_o_u_t.  The _s_p_r_i_n_t_f() function shall place output followed by the null
       byte, ''\\00'', in consecutive bytes starting at *_s; it is the user's
       responsibility to ensure that enough space is available.

       The _d_p_r_i_n_t_f() function shall be equivalent to the _f_p_r_i_n_t_f() function,
       except that _d_p_r_i_n_t_f() shall write output to the file associated with
       the file descriptor specified by the _f_i_l_d_e_s argument rather than place
       output on a stream.

       The _s_n_p_r_i_n_t_f() function shall be equivalent to _s_p_r_i_n_t_f(), with the
       addition of the _n argument which states the size of the buffer referred
       to by _s.  If _n is zero, nothing shall be written and _s may be a null
       pointer. Otherwise, output bytes beyond the _n-1st shall be 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.

       If copying takes place between objects that overlap as a result of a
       call to _s_p_r_i_n_t_f() or _s_n_p_r_i_n_t_f(), the results are undefined.

       Each of these functions converts, formats, and prints its arguments
       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: _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, and _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 shall result in the fetching of zero or more arguments.  The
       results are undefined if there are insufficient arguments for the
       _f_o_r_m_a_t.  If the _f_o_r_m_a_t is exhausted while arguments remain, the excess
       arguments shall be evaluated but are otherwise ignored.

       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 specifier 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).

       The _f_o_r_m_a_t can contain either numbered argument conversion
       specifications (that is, "%_n$" and "*_m$"), or unnumbered argument
       conversion specifications (that is, %% and **), but 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 numbered 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.

       In format strings containing the "%_n$" form of conversion
       specification, 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 specification,
       each conversion specification uses the first unused argument in the
       argument list.

       All forms of the _f_p_r_i_n_t_f() functions allow for the insertion of a
       language-dependent radix character in the output string. The radix
       character is defined in the current locale (category _L_C___N_U_M_E_R_I_C).  In
       the POSIX locale, or in a locale where the radix character is not
       defined, the radix character shall default to a <period> (''..'').

       Each conversion specification is introduced by the ''%%'' character or by
       the character sequence "%_n$", after which the following appear in
       sequence:

        *  Zero or more _f_l_a_g_s (in any order), which modify the meaning of the
           conversion specification.

        *  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 shall be padded with <space>
           characters by default on the left; it shall 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.

        *  An optional _p_r_e_c_i_s_i_o_n that gives the minimum number of digits to
           appear for the dd, ii, oo, uu, xx, and XX conversion specifiers; the
           number of digits to appear after the radix character for the aa, AA,
           ee, EE, ff, and FF conversion specifiers; the maximum number of
           significant digits for the gg and GG conversion specifiers; or the
           maximum number of bytes to be printed from a string in the ss and SS
           conversion specifiers. 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 zero. If a precision appears with any other conversion
           specifier, the behavior is undefined.

        *  An optional length modifier that specifies the size of the
           argument.

        *  A _c_o_n_v_e_r_s_i_o_n _s_p_e_c_i_f_i_e_r character that indicates the type of
           conversion to be applied.

       A field width, or precision, or both, may be indicated by an <asterisk>
       (''**'').  In this case an argument of type iinntt supplies the field width
       or precision. Applications shall ensure that arguments specifying field
       width, or precision, or both appear in that order before the argument,
       if any, to be converted. A negative field width is taken as a ''--'' flag
       followed by a positive field width. A negative precision is taken as if
       the precision were omitted.  In _f_o_r_m_a_t 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 _f_o_r_m_a_t 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 flag characters and their meanings are:

       '       (The <apostrophe>.)  The integer portion of the result of a
               decimal conversion (%%ii, %%dd, %%uu, %%ff, %%FF, %%gg, or %%GG) shall 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 shall be left-justified within the
               field.  The conversion is right-justified if this flag is not
               specified.

       +       The result of a signed conversion shall always begin with a
               sign (''++'' or ''--'').  The conversion shall 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 or
               if a signed conversion results in no characters, a <space>
               shall be prefixed to the result. This means that if the <space>
               and ''++'' flags both appear, the <space> flag shall be ignored.

       #       Specifies that the value is to be converted to an alternative
               form. For oo conversion, it shall increase the precision, if and
               only if necessary, to force the first digit of the result to be
               a zero (if the value and precision are both 0, a single 0 is
               printed). For xx or XX conversion specifiers, a non-zero result
               shall have 0x (or 0X) prefixed to it. For aa, AA, ee, EE, ff, FF, gg,
               and GG conversion specifiers, the result shall always contain a
               radix character, even if no digits follow the radix character.
               Without this flag, a radix character appears in the result of
               these conversions only if a digit follows it. For gg and GG
               conversion specifiers, trailing zeros shall _n_o_t be removed from
               the result as they normally are. For other conversion
               specifiers, the behavior is undefined.

       0       For dd, ii, oo, uu, xx, XX, aa, AA, ee, EE, ff, FF, gg, and GG conversion
               specifiers, leading zeros (following any indication of sign or
               base) are used to pad to the field width rather than performing
               space padding, except when converting an infinity or NaN. If
               the ''00'' and ''--'' flags both appear, the ''00'' flag is ignored. For
               dd, ii, oo, uu, xx, and XX conversion specifiers, if a precision is
               specified, the ''00'' flag shall be ignored.  If the ''00'' and
               <apostrophe> flags both appear, the grouping characters are
               inserted before zero padding. For other conversions, the
               behavior is undefined.

       The length modifiers and their meanings are:

       hh      Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a ssiiggnneedd cchhaarr or uunnssiiggnneedd cchhaarr argument
               (the argument will have been promoted according to the integer
               promotions, but its value shall be converted to ssiiggnneedd cchhaarr or
               uunnssiiggnneedd cchhaarr before printing); or that a following nn
               conversion specifier applies to a pointer to a ssiiggnneedd cchhaarr
               argument.

       h       Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a sshhoorrtt or uunnssiiggnneedd sshhoorrtt argument (the
               argument will have been promoted according to the integer
               promotions, but its value shall be converted to sshhoorrtt or
               uunnssiiggnneedd sshhoorrtt before printing); or that a following nn
               conversion specifier applies to a pointer to a sshhoorrtt argument.

       l (ell) Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a lloonngg or uunnssiiggnneedd lloonngg argument; that a
               following nn conversion specifier applies to a pointer to a lloonngg
               argument; that a following cc conversion specifier applies to a
               wwiinntt__tt argument; that a following ss conversion specifier
               applies to a pointer to a wwcchhaarr__tt argument; or has no effect on
               a following aa, AA, ee, EE, ff, FF, gg, or GG conversion specifier.

       ll (ell-ell)
               Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a lloonngg lloonngg or uunnssiiggnneedd lloonngg lloonngg
               argument; or that a following nn conversion specifier applies to
               a pointer to a lloonngg lloonngg argument.

       j       Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to an iinnttmmaaxx__tt or uuiinnttmmaaxx__tt argument; or that
               a following nn conversion specifier applies to a pointer to an
               iinnttmmaaxx__tt argument.

       z       Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a ssiizzee__tt or the corresponding signed
               integer type argument; or that a following nn conversion
               specifier applies to a pointer to a signed integer type
               corresponding to a ssiizzee__tt argument.

       t       Specifies that a following dd, ii, oo, uu, xx, or XX conversion
               specifier applies to a ppttrrddiiffff__tt or the corresponding uunnssiiggnneedd
               type argument; or that a following nn conversion specifier
               applies to a pointer to a ppttrrddiiffff__tt argument.

       L       Specifies that a following aa, AA, ee, EE, ff, FF, gg, or GG conversion
               specifier applies to a lloonngg ddoouubbllee argument.

       If a length modifier appears with any conversion specifier other than
       as specified above, the behavior is undefined.

       The conversion specifiers and their meanings are:

       d, i    The iinntt argument shall be 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 shall be expanded with leading
               zeros. The default precision is 1. The result of converting
               zero with an explicit precision of zero shall be no characters.

       o       The uunnssiiggnneedd argument shall be 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 shall be expanded with leading
               zeros. The default precision is 1. The result of converting
               zero with an explicit precision of zero shall be no characters.

       u       The uunnssiiggnneedd argument shall be 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 shall be expanded with leading
               zeros. The default precision is 1. The result of converting
               zero with an explicit precision of zero shall be no characters.

       x       The uunnssiiggnneedd argument shall be converted to unsigned
               hexadecimal format in the style "_d_d_d_d"; the letters ""aabbccddeeff""
               are used. The precision specifies the minimum number of digits
               to appear; if the value being converted can be represented in
               fewer digits, it shall be expanded with leading zeros. The
               default precision is 1. The result of converting zero with an
               explicit precision of zero shall be no characters.

       X       Equivalent to the xx conversion specifier, except that letters
               ""AABBCCDDEEFF"" are used instead of ""aabbccddeeff"".

       f, F    The ddoouubbllee argument shall be converted to decimal notation in
               the style "[-]_d_d_d._d_d_d", where the number of digits after the
               radix character is equal to the precision specification. If the
               precision is missing, it shall be taken as 6; if the precision
               is explicitly zero and no ''##'' flag is present, no radix
               character shall appear. If a radix character appears, at least
               one digit appears before it. The low-order digit shall be
               rounded in an implementation-defined manner.

               A ddoouubbllee argument representing an infinity shall be converted
               in one of the styles ""[[--]]iinnff"" or ""[[--]]iinnffiinniittyy""; which style is
               implementation-defined. A ddoouubbllee argument representing a NaN
               shall be converted in one of the styles "[-]nan(_n_-_c_h_a_r_-
               _s_e_q_u_e_n_c_e)" or ""[[--]]nnaann""; which style, and the meaning of any _n_-
               _c_h_a_r_-_s_e_q_u_e_n_c_e, is implementation-defined. The FF conversion
               specifier produces ""IINNFF"", ""IINNFFIINNIITTYY"", or ""NNAANN"" instead of
               ""iinnff"", ""iinnffiinniittyy"", or ""nnaann"", respectively.

       e, E    The ddoouubbllee argument shall be converted in 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; if the
               precision is missing, it shall be taken as 6; if the precision
               is zero and no ''##'' flag is present, no radix character shall
               appear. The low-order digit shall be rounded in an
               implementation-defined manner. The EE conversion specifier shall
               produce a number with ''EE'' instead of ''ee'' introducing the
               exponent. The exponent shall always contain at least two
               digits. If the value is zero, the exponent shall be zero.

               A ddoouubbllee argument representing an infinity or NaN shall be
               converted in the style of an ff or FF conversion specifier.

       g, G    The ddoouubbllee argument representing a floating-point number shall
               be converted in the style ff or ee (or in the style FF or EE in the
               case of a GG conversion specifier), depending on the value
               converted and the precision.  Let PP equal the precision if non-
               zero, 6 if the precision is omitted, or 1 if the precision is
               zero. Then, if a conversion with style EE would have an exponent
               of _X:

               --  If PP>_X>=-4, the conversion shall be with style ff (or FF) and
                   precision PP-(_X+1).

               --  Otherwise, the conversion shall be with style ee (or EE) and
                   precision PP-1.

               Finally, unless the ''##'' flag is used, any trailing zeros shall
               be removed from the fractional portion of the result and the
               decimal-point character shall be removed if there is no
               fractional portion remaining.

               A ddoouubbllee argument representing an infinity or NaN shall be
               converted in the style of an ff or FF conversion specifier.

       a, A    A ddoouubbllee argument representing a floating-point number shall be
               converted in the style "[-]0x_h._h_h_h_hp+-_d", where there is one
               hexadecimal digit (which shall be non-zero if the argument is a
               normalized floating-point number and is otherwise unspecified)
               before the decimal-point character and the number of
               hexadecimal digits after it is equal to the precision; if the
               precision is missing and FLT_RADIX is a power of 2, then the
               precision shall be sufficient for an exact representation of
               the value; if the precision is missing and FLT_RADIX is not a
               power of 2, then the precision shall be sufficient to
               distinguish values of type ddoouubbllee, except that trailing zeros
               may be omitted; if the precision is zero and the ''##'' flag is
               not specified, no decimal-point character shall appear. The
               letters ""aabbccddeeff"" shall be used for aa conversion and the letters
               ""AABBCCDDEEFF"" for AA conversion. The AA conversion specifier produces
               a number with ''XX'' and ''PP'' instead of ''xx'' and ''pp''.  The exponent
               shall 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 shall be zero.

               A ddoouubbllee argument representing an infinity or NaN shall be
               converted in the style of an ff or FF conversion specifier.

       c       The iinntt argument shall be converted to an uunnssiiggnneedd cchhaarr, and
               the resulting byte shall be written.

               If an ll (ell) qualifier is present, the wwiinntt__tt argument shall
               be converted as if by an llss conversion specification with no
               precision and an argument that points to a two-element array of
               type wwcchhaarr__tt, the first element of which contains the wwiinntt__tt
               argument to the llss conversion specification and the second
               element contains a null wide character.

       s       The argument shall be a pointer to an array of cchhaarr.  Bytes
               from the array shall be written up to (but not including) any
               terminating null byte. If the precision is specified, no more
               than that many bytes shall be written. If the precision is not
               specified or is greater than the size of the array, the
               application shall ensure that the array contains a null byte.

               If an ll (ell) qualifier is present, the argument shall be a
               pointer to an array of type wwcchhaarr__tt.  Wide characters from the
               array shall be converted to characters (each as if by a call to
               the _w_c_r_t_o_m_b() function, with the conversion state described by
               an mmbbssttaattee__tt object initialized to zero before the first wide
               character is converted) up to and including a terminating null
               wide character. The resulting characters shall be written up to
               (but not including) the terminating null character (byte). If
               no precision is specified, the application shall ensure that
               the array contains a null wide character.  If a precision is
               specified, no more than that many characters (bytes) shall be
               written (including shift sequences, if any), and the array
               shall 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 shall a partial character be written.

       p       The argument shall be a pointer to vvooiidd.  The value of the
               pointer is converted to a sequence of printable characters, in
               an implementation-defined manner.

       n       The argument shall be a pointer to an integer into which is
               written the number of bytes written to the output so far by
               this call to one of the _f_p_r_i_n_t_f() functions. No argument is
               converted.

       C       Equivalent to llcc.

       S       Equivalent to llss.

       %       Print a ''%%'' character; no argument is converted. The complete
               conversion specification shall be %%%%.

       If a conversion specification does not match one of the above forms,
       the behavior is undefined. If any argument is not the correct type for
       the corresponding conversion specification, the behavior is undefined.

       In no case shall a nonexistent or small field width cause truncation of
       a field; if the result of a conversion is wider than the field width,
       the field shall be expanded to contain the conversion result.
       Characters generated by _f_p_r_i_n_t_f() and _p_r_i_n_t_f() are printed as if
       _f_p_u_t_c() had been called.

       For the aa and AA conversion specifiers, if FLT_RADIX is a power of 2,
       the value shall be correctly rounded to a hexadecimal floating number
       with the given precision.

       For aa and AA conversions, if FLT_RADIX is not a power of 2 and the
       result is not exactly representable in the given precision, the result
       should be one of the two adjacent numbers in hexadecimal floating style
       with the given precision, with the extra stipulation that the error
       should have a correct sign for the current rounding direction.

       For the ee, EE, ff, FF, gg, and GG conversion specifiers, if the number of
       significant decimal digits is at most DECIMAL_DIG, then the result
       should be correctly rounded. If the number of significant decimal
       digits is more than DECIMAL_DIG but the source value is exactly
       representable with DECIMAL_DIG digits, then the result should be an
       exact representation with trailing zeros.  Otherwise, the source value
       is bounded by two adjacent decimal strings _L < _U, both having
       DECIMAL_DIG significant digits; the value of the resultant decimal
       string _D should satisfy _L <= _D <= _U, with the extra stipulation that
       the error should have a correct sign for the current rounding
       direction.

       The last data modification and last file status change timestamps of
       the file shall be marked for update:

        1. Between the call to a successful execution of _f_p_r_i_n_t_f() or _p_r_i_n_t_f()
           and the next successful completion of a call to _f_f_l_u_s_h() or
           _f_c_l_o_s_e() on the same stream or a call to _e_x_i_t() or _a_b_o_r_t()

        2. Upon successful completion of a call to _d_p_r_i_n_t_f()

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _d_p_r_i_n_t_f(), _f_p_r_i_n_t_f(), and _p_r_i_n_t_f()
       functions shall return the number of bytes transmitted.

       Upon successful completion, the _s_p_r_i_n_t_f() function shall return the
       number of bytes written to _s, excluding the terminating null byte.

       Upon successful completion, the _s_n_p_r_i_n_t_f() function shall return the
       number of bytes that would be written to _s had _n been sufficiently
       large excluding the terminating null byte.

       If an output error was encountered, these functions shall return a
       negative value and set _e_r_r_n_o to indicate the error.

       If the value of _n is zero on a call to _s_n_p_r_i_n_t_f(), nothing shall be
       written, the number of bytes that would have been written had _n been
       sufficiently large excluding the terminating null shall be returned,
       and _s may be a null pointer.

EERRRROORRSS
       For the conditions under which _d_p_r_i_n_t_f(), _f_p_r_i_n_t_f(), and _p_r_i_n_t_f() fail
       and may fail, refer to _f_p_u_t_c() or _f_p_u_t_w_c().

       In addition, all forms of _f_p_r_i_n_t_f() shall fail if:

       EEIILLSSEEQQ A wide-character code that does not correspond to a valid
              character has been detected.

       EEOOVVEERRFFLLOOWW
              The value to be returned is greater than {INT_MAX}.

       The _d_p_r_i_n_t_f() function may fail if:

       EEBBAADDFF  The _f_i_l_d_e_s argument is not a valid file descriptor.

       The _d_p_r_i_n_t_f(), _f_p_r_i_n_t_f(), and _p_r_i_n_t_f() functions may fail if:

       EENNOOMMEEMM Insufficient storage space is available.

       The _s_n_p_r_i_n_t_f() function shall fail if:

       EEOOVVEERRFFLLOOWW
              The value of _n is greater than {INT_MAX}.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   PPrriinnttiinngg LLaanngguuaaggee--IInnddeeppeennddeenntt DDaattee aanndd TTiimmee
       The following statement can be used to print date and time using a
       language-independent format:


           printf(format, weekday, month, day, hour, min);

       For American usage, _f_o_r_m_a_t could be a pointer to the following string:


           "%s, %s %d, %d:%.2d\n"

       This example would produce the following message:


           Sunday, July 3, 10:02

       For German usage, _f_o_r_m_a_t could be a pointer to the following string:


           "%1$s, %3$d. %2$s, %4$d:%5$.2d\n"

       This definition of _f_o_r_m_a_t would produce the following message:


           Sonntag, 3. Juli, 10:02

   PPrriinnttiinngg FFiillee IInnffoorrmmaattiioonn
       The following example prints information about the type, permissions,
       and number of links of a specific file in a directory.

       The first two calls to _p_r_i_n_t_f() use data decoded from a previous _s_t_a_t()
       call. The user-defined _s_t_r_p_e_r_m() function shall return a string similar
       to the one at the beginning of the output for the following command:


           ls -l

       The next call to _p_r_i_n_t_f() outputs the owner's name if it is found using
       _g_e_t_p_w_u_i_d(); the _g_e_t_p_w_u_i_d() function shall return a ppaasssswwdd structure
       from which the name of the user is extracted. If the user name is not
       found, the program instead prints out the numeric value of the user ID.

       The next call prints out the group name if it is found using
       _g_e_t_g_r_g_i_d(); _g_e_t_g_r_g_i_d() is very similar to _g_e_t_p_w_u_i_d() except that it
       shall return group information based on the group number.  Once again,
       if the group is not found, the program prints the numeric value of the
       group for the entry.

       The final call to _p_r_i_n_t_f() prints the size of the file.


           #include <stdio.h>
           #include <sys/types.h>
           #include <pwd.h>
           #include <grp.h>

           char *strperm (mode_t);
           ...
           struct stat statbuf;
           struct passwd *pwd;
           struct group *grp;
           ...
           printf("%10.10s", strperm (statbuf.st_mode));
           printf("%4d", statbuf.st_nlink);

           if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
               printf(" %-8.8s", pwd->pw_name);
           else
               printf(" %-8ld", (long) statbuf.st_uid);

           if ((grp = getgrgid(statbuf.st_gid)) != NULL)
               printf(" %-8.8s", grp->gr_name);
           else
               printf(" %-8ld", (long) statbuf.st_gid);

           printf("%9jd", (intmax_t) statbuf.st_size);
           ...

   PPrriinnttiinngg aa LLooccaalliizzeedd DDaattee SSttrriinngg
       The following example gets a localized date string. The _n_l___l_a_n_g_i_n_f_o()
       function shall return the localized date string, which specifies the
       order and layout of the date. The _s_t_r_f_t_i_m_e() function takes this
       information and, using the ttmm structure for values, places the date and
       time information into _d_a_t_e_s_t_r_i_n_g.  The _p_r_i_n_t_f() function then outputs
       _d_a_t_e_s_t_r_i_n_g and the name of the entry.


           #include <stdio.h>
           #include <time.h>
           #include <langinfo.h>
           ...
           struct dirent *dp;
           struct tm *tm;
           char datestring[256];
           ...
           strftime(datestring, sizeof(datestring), nl_langinfo (D_T_FMT), tm);

           printf(" %s %s\n", datestring, dp->d_name);
           ...

   PPrriinnttiinngg EErrrroorr IInnffoorrmmaattiioonn
       The following example uses _f_p_r_i_n_t_f() to write error information to
       standard error.

       In the first group of calls, the program tries to open the password
       lock file named LLOOCCKKFFIILLEE.  If the file already exists, this is an
       error, as indicated by the O_EXCL flag on the _o_p_e_n() function. If the
       call fails, the program assumes that someone else is updating the
       password file, and the program exits.

       The next group of calls saves a new password file as the current
       password file by creating a link between LLOOCCKKFFIILLEE and the new password
       file PPAASSSSWWDDFFIILLEE.


           #include <sys/types.h>
           #include <sys/stat.h>
           #include <fcntl.h>
           #include <stdio.h>
           #include <stdlib.h>
           #include <unistd.h>
           #include <string.h>
           #include <errno.h>

           #define LOCKFILE "/etc/ptmp"
           #define PASSWDFILE "/etc/passwd"
           ...
           int pfd;
           ...
           if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
               S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
           {
               fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\n");
               exit(1);
           }
           ...
           if (link(LOCKFILE,PASSWDFILE) == -1) {
               fprintf(stderr, "Link error: %s\n", strerror(errno));
               exit(1);
           }
           ...

   PPrriinnttiinngg UUssaaggee IInnffoorrmmaattiioonn
       The following example checks to make sure the program has the necessary
       arguments, and uses _f_p_r_i_n_t_f() to print usage information if the
       expected number of arguments is not present.


           #include <stdio.h>
           #include <stdlib.h>
           ...
           char *Options = "hdbtl";
           ...
           if (argc < 2) {
               fprintf(stderr, "Usage: %s -%s <file\n", argv[0], Options); exit(1);
           }
           ...

   FFoorrmmaattttiinngg aa DDeecciimmaall SSttrriinngg
       The following example prints a key and data pair on _s_t_d_o_u_t.  Note use
       of the <asterisk> (''**'') in the format string; this ensures the correct
       number of decimal places for the element based on the number of
       elements requested.


           #include <stdio.h>
           ...
           long i;
           char *keystr;
           int elementlen, len;
           ...
           while (len < elementlen) {
           ...
               printf("%s Element%0*ld\n", keystr, elementlen, i);
           ...
           }

   CCrreeaattiinngg aa PPaatthhnnaammee
       The following example creates a pathname using information from a
       previous _g_e_t_p_w_n_a_m() function that returned the password database entry
       of the user.


           #include <stdint.h>
           #include <stdio.h>
           #include <stdlib.h>
           #include <string.h>
           #include <sys/types.h>
           #include <unistd.h>
           ...
           char *pathname;
           struct passwd *pw;
           size_t len;
           ...
           // digits required for pid_t is number of bits times
           // log2(10) = approx 10/33
           len = strlen(pw->pw_dir) + 1 + 1+(sizeof(pid_t)*80+32)/33 +
               sizeof ".out";
           pathname = malloc(len);
           if (pathname != NULL)
           {
               snprintf(pathname, len, "%s/%jd.out", pw->pw_dir,
                   (intmax_t)getpid());
               ...
           }

   RReeppoorrttiinngg aann EEvveenntt
       The following example loops until an event has timed out. The _p_a_u_s_e()
       function waits forever unless it receives a signal. The _f_p_r_i_n_t_f()
       statement should never occur due to the possible return values of
       _p_a_u_s_e().


           #include <stdio.h>
           #include <unistd.h>
           #include <string.h>
           #include <errno.h>
           ...
           while (!event_complete) {
           ...
               if (pause() != -1 || errno != EINTR)
                   fprintf(stderr, "pause: unknown error: %s\n", strerror(errno));
           }
           ...

   PPrriinnttiinngg MMoonneettaarryy IInnffoorrmmaattiioonn
       The following example uses _s_t_r_f_m_o_n() to convert a number and store it
       as a formatted monetary string named _c_o_n_v_b_u_f.  If the first number is
       printed, the program prints the format and the description; otherwise,
       it just prints the number.


           #include <monetary.h>
           #include <stdio.h>
           ...
           struct tblfmt {
               char *format;
               char *description;
           };

           struct tblfmt table[] = {
               { "%n", "default formatting" },
               { "%11n", "right align within an 11 character field" },
               { "%#5n", "aligned columns for values up to 99999" },
               { "%=*#5n", "specify a fill character" },
               { "%=0#5n", "fill characters do not use grouping" },
               { "%^#5n", "disable the grouping separator" },
               { "%^#5.0n", "round off to whole units" },
               { "%^#5.4n", "increase the precision" },
               { "%(#5n", "use an alternative pos/neg style" },
               { "%!(#5n", "disable the currency symbol" },
           };
           ...
           float input[3];
           int i, j;
           char convbuf[100];
           ...
           strfmon(convbuf, sizeof(convbuf), table[i].format, input[j]);

           if (j == 0) {
               printf("%s%s%s\n", table[i].format,
                   convbuf, table[i].description);
           }
           else {
               printf("%s\n", convbuf);
           }
           ...

   PPrriinnttiinngg WWiiddee CChhaarraacctteerrss
       The following example prints a series of wide characters. Suppose that
       ""LL``@@``"" expands to three bytes:


           wchar_t wz [3] = L"@@";       // Zero-terminated
           wchar_t wn [3] = L"@@@";      // Unterminated

           fprintf (stdout,"%ls", wz);   // Outputs 6 bytes
           fprintf (stdout,"%ls", wn);   // Undefined because wn has no terminator
           fprintf (stdout,"%4ls", wz);  // Outputs 3 bytes
           fprintf (stdout,"%4ls", wn);  // Outputs 3 bytes; no terminator needed
           fprintf (stdout,"%9ls", wz);  // Outputs 6 bytes
           fprintf (stdout,"%9ls", wn);  // Outputs 9 bytes; no terminator needed
           fprintf (stdout,"%10ls", wz); // Outputs 6 bytes
           fprintf (stdout,"%10ls", wn); // Undefined because wn has no terminator

       In the last line of the example, after processing three characters,
       nine bytes have been output. The fourth character must then be examined
       to determine whether it converts to one byte or more. If it converts to
       more than one byte, the output is only nine bytes. Since there is no
       fourth character in the array, the behavior is undefined.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       If the application calling _f_p_r_i_n_t_f() has any objects of type wwiinntt__tt or
       wwcchhaarr__tt, it must also include the _<_w_c_h_a_r_._h_> header to have these
       objects defined.

RRAATTIIOONNAALLEE
       If an implementation detects that there are insufficient arguments for
       the format, it is recommended that the function should fail and report
       an [[EEIINNVVAALL]] error.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._5, _S_t_a_n_d_a_r_d _I_/_O _S_t_r_e_a_m_s, _f_p_u_t_c(), _f_s_c_a_n_f(), _s_e_t_l_o_c_a_l_e(),
       _s_t_r_f_m_o_n(), _w_c_r_t_o_m_b()

       The Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _7, _L_o_c_a_l_e,
       <<iinnttttyyppeess..hh>>, <<ssttddiioo..hh>>, <<wwcchhaarr..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                           FPRINTF(3)
