PRINTF(1)                  POSIX Programmer's Manual                 PRINTF(1)



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
       printf -- write formatted output

SSYYNNOOPPSSIISS
       printf _f_o_r_m_a_t [[_a_r_g_u_m_e_n_t...]]

DDEESSCCRRIIPPTTIIOONN
       The _p_r_i_n_t_f utility shall write formatted operands to the standard
       output. The _a_r_g_u_m_e_n_t operands shall be formatted under control of the
       _f_o_r_m_a_t operand.

OOPPTTIIOONNSS
       None.

OOPPEERRAANNDDSS
       The following operands shall be supported:

       _f_o_r_m_a_t    A string describing the format to use to write the remaining
                 operands.  See the EXTENDED DESCRIPTION section.

       _a_r_g_u_m_e_n_t  The strings to be written to standard output, under the
                 control of _f_o_r_m_a_t.  See the EXTENDED DESCRIPTION section.

SSTTDDIINN
       Not used.

IINNPPUUTT FFIILLEESS
       None.

EENNVVIIRROONNMMEENNTT VVAARRIIAABBLLEESS
       The following environment variables shall affect the execution of
       _p_r_i_n_t_f:

       _L_A_N_G      Provide a default value for the internationalization
                 variables that are unset or null. (See the Base Definitions
                 volume of POSIX.1-2017, _S_e_c_t_i_o_n _8_._2, _I_n_t_e_r_n_a_t_i_o_n_a_l_i_z_a_t_i_o_n
                 _V_a_r_i_a_b_l_e_s the precedence of internationalization variables
                 used to determine the values of locale categories.)

       _L_C___A_L_L    If set to a non-empty string value, override the values of
                 all the other internationalization variables.

       _L_C___C_T_Y_P_E  Determine the locale for the interpretation of sequences of
                 bytes of text data as characters (for example, single-byte as
                 opposed to multi-byte characters in arguments).

       _L_C___M_E_S_S_A_G_E_S
                 Determine the locale that should be used to affect the format
                 and contents of diagnostic messages written to standard
                 error.

       _L_C___N_U_M_E_R_I_C
                 Determine the locale for numeric formatting. It shall affect
                 the format of numbers written using the ee, EE, ff, gg, and GG
                 conversion specifier characters (if supported).

       _N_L_S_P_A_T_H   Determine the location of message catalogs for the processing
                 of _L_C___M_E_S_S_A_G_E_S.

AASSYYNNCCHHRROONNOOUUSS EEVVEENNTTSS
       Default.

SSTTDDOOUUTT
       See the EXTENDED DESCRIPTION section.

SSTTDDEERRRR
       The standard error shall be used only for diagnostic messages.

OOUUTTPPUUTT FFIILLEESS
       None.

EEXXTTEENNDDEEDD DDEESSCCRRIIPPTTIIOONN
       The _f_o_r_m_a_t operand shall be used as the _f_o_r_m_a_t string described in the
       Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _5, _F_i_l_e _F_o_r_m_a_t
       _N_o_t_a_t_i_o_n with the following exceptions:

        1. A <space> in the format string, in any context other than a flag of
           a conversion specification, shall be treated as an ordinary
           character that is copied to the output.

        2. A ''' character in the format string shall be treated as a '''
           character, not as a <space>.

        3. In addition to the escape sequences shown in the Base Definitions
           volume of POSIX.1-2017, _C_h_a_p_t_e_r _5, _F_i_l_e _F_o_r_m_a_t _N_o_t_a_t_i_o_n (''\\\\'',
           ''\\aa'', ''\\bb'', ''\\ff'', ''\\nn'', ''\\rr'', ''\\tt'', ''\\vv''), ""\\dddddd"", where _d_d_d is a
           one, two, or three-digit octal number, shall be written as a byte
           with the numeric value specified by the octal number.

        4. The implementation shall not precede or follow output from the dd or
           uu conversion specifiers with <blank> characters not specified by
           the _f_o_r_m_a_t operand.

        5. The implementation shall not precede output from the oo conversion
           specifier with zeros not specified by the _f_o_r_m_a_t operand.

        6. The aa, AA, ee, EE, ff, FF, gg, and GG conversion specifiers need not be
           supported.

        7. An additional conversion specifier character, bb, shall be supported
           as follows. The argument shall be taken to be a string that can
           contain <backslash>-escape sequences. The following
           <backslash>-escape sequences shall be supported:

           --  The escape sequences listed in the Base Definitions volume of
               POSIX.1-2017, _C_h_a_p_t_e_r _5, _F_i_l_e _F_o_r_m_a_t _N_o_t_a_t_i_o_n (''\\\\'', ''\\aa'',
               ''\\bb'', ''\\ff'', ''\\nn'', ''\\rr'', ''\\tt'', ''\\vv''), which shall be converted
               to the characters they represent

           --  ""\\00dddddd"", where _d_d_d is a zero, one, two, or three-digit octal
               number that shall be converted to a byte with the numeric value
               specified by the octal number

           --  ''\\cc'', which shall not be written and shall cause _p_r_i_n_t_f to
               ignore any remaining characters in the string operand
               containing it, any remaining string operands, and any
               additional characters in the _f_o_r_m_a_t operand

           The interpretation of a <backslash> followed by any other sequence
           of characters is unspecified.

           Bytes from the converted string shall be written until the end of
           the string or the number of bytes indicated by the precision
           specification is reached. If the precision is omitted, it shall be
           taken to be infinite, so all bytes up to the end of the converted
           string shall be written.

        8. For each conversion specification that consumes an argument, the
           next _a_r_g_u_m_e_n_t operand shall be evaluated and converted to the
           appropriate type for the conversion as specified below.

        9. The _f_o_r_m_a_t operand shall be reused as often as necessary to satisfy
           the _a_r_g_u_m_e_n_t operands. Any extra bb, cc, or ss conversion specifiers
           shall be evaluated as if a null string argument were supplied;
           other extra conversion specifications shall be evaluated as if a
           zero argument were supplied. If the _f_o_r_m_a_t operand contains no
           conversion specifications and _a_r_g_u_m_e_n_t operands are present, the
           results are unspecified.

       10. If a character sequence in the _f_o_r_m_a_t operand begins with a ''%%''
           character, but does not form a valid conversion specification, the
           behavior is unspecified.

       11. The argument to the cc conversion specifier can be a string
           containing zero or more bytes. If it contains one or more bytes,
           the first byte shall be written and any additional bytes shall be
           ignored. If the argument is an empty string, it is unspecified
           whether nothing is written or a null byte is written.

       The _a_r_g_u_m_e_n_t operands shall be treated as strings if the corresponding
       conversion specifier is bb, cc, or ss, and shall be evaluated as if by the
       _s_t_r_t_o_d() function if the corresponding conversion specifier is aa, AA, ee,
       EE, ff, FF, gg, or GG.  Otherwise, they shall be evaluated as unsuffixed C
       integer constants, as described by the ISO C standard, with the
       following extensions:

        *  A leading <plus-sign> or <hyphen-minus> shall be allowed.

        *  If the leading character is a single-quote or double-quote, the
           value shall be the numeric value in the underlying codeset of the
           character following the single-quote or double-quote.

        *  Suffixed integer constants may be allowed.

       If an _a_r_g_u_m_e_n_t operand cannot be completely converted into an internal
       value appropriate to the corresponding conversion specification, a
       diagnostic message shall be written to standard error and the utility
       shall not exit with a zero exit status, but shall continue processing
       any remaining operands and shall write the value accumulated at the
       time the error was detected to standard output.

       It shall not be considered an error if an _a_r_g_u_m_e_n_t operand is not
       completely used for a bb, cc, or ss conversion.

EEXXIITT SSTTAATTUUSS
       The following exit values shall be returned:

        0    Successful completion.

       >0    An error occurred.

CCOONNSSEEQQUUEENNCCEESS OOFF EERRRROORRSS
       Default.

       _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_.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The floating-point formatting conversion specifications of _p_r_i_n_t_f() are
       not required because all arithmetic in the shell is integer arithmetic.
       The _a_w_k utility performs floating-point calculations and provides its
       own pprriinnttff function. The _b_c utility can perform arbitrary-precision
       floating-point arithmetic, but does not provide extensive formatting
       capabilities. (This _p_r_i_n_t_f utility cannot really be used to format _b_c
       output; it does not support arbitrary precision.) Implementations are
       encouraged to support the floating-point conversions as an extension.

       Note that this _p_r_i_n_t_f utility, like the _p_r_i_n_t_f() function defined in
       the System Interfaces volume of POSIX.1-2017 on which it is based,
       makes no special provision for dealing with multi-byte characters when
       using the %%cc conversion specification or when a precision is specified
       in a %%bb or %%ss conversion specification. Applications should be
       extremely cautious using either of these features when there are multi-
       byte characters in the character set.

       No provision is made in this volume of POSIX.1-2017 which allows field
       widths and precisions to be specified as ''**'' since the ''**'' can be
       replaced directly in the _f_o_r_m_a_t operand using shell variable
       substitution. Implementations can also provide this feature as an
       extension if they so choose.

       Hexadecimal character constants as defined in the ISO C standard are
       not recognized in the _f_o_r_m_a_t operand because there is no consistent way
       to detect the end of the constant. Octal character constants are
       limited to, at most, three octal digits, but hexadecimal character
       constants are only terminated by a non-hex-digit character. In the
       ISO C standard, the ""####"" concatenation operator can be used to
       terminate a constant and follow it with a hexadecimal character to be
       written. In the shell, concatenation occurs before the _p_r_i_n_t_f utility
       has a chance to parse the end of the hexadecimal constant.

       The %%bb conversion specification is not part of the ISO C standard; it
       has been added here as a portable way to process <backslash>-escapes
       expanded in string operands as provided by the _e_c_h_o utility. See also
       the APPLICATION USAGE section of _e_c_h_o for ways to use _p_r_i_n_t_f as a
       replacement for all of the traditional versions of the _e_c_h_o utility.

       If an argument cannot be parsed correctly for the corresponding
       conversion specification, the _p_r_i_n_t_f utility is required to report an
       error. Thus, overflow and extraneous characters at the end of an
       argument being used for a numeric conversion shall be reported as
       errors.

EEXXAAMMPPLLEESS
       To alert the user and then print and read a series of prompts:


           printf "\aPlease fill in the following: \nName: "
           read name
           printf "Phone number: "
           read phone

       To read out a list of right and wrong answers from a file, calculate
       the percentage correctly, and print them out. The numbers are right-
       justified and separated by a single <tab>.  The percentage is written
       to one decimal place of accuracy:


           while read right wrong ; do
               percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
               printf "%2d right\t%2d wrong\t(%s%%)\n" \
                   $right $wrong $percent
           done < database_file

       The command:


           printf "%5d%4d\n" 1 21 321 4321 54321

       produces:


               1  21
             3214321
           54321   0

       Note that the _f_o_r_m_a_t operand is used three times to print all of the
       given strings and that a ''00'' was supplied by _p_r_i_n_t_f to satisfy the last
       %%44dd conversion specification.

       The _p_r_i_n_t_f utility is required to notify the user when conversion
       errors are detected while producing numeric output; thus, the following
       results would be expected on an implementation with 32-bit twos-
       complement integers when %%dd is specified as the _f_o_r_m_a_t operand:

       +------------+-------------+-------------------------------------------+
       |            |  SSttaannddaarrdd   |                                           |
       | AArrgguummeenntt   |   OOuuttppuutt    |             DDiiaaggnnoossttiicc OOuuttppuutt             |
       +------------+-------------+-------------------------------------------+
       |5a          | 5           | printf: "5a" not completely converted     |
       |9999999999  | 2147483647  | printf: "9999999999" arithmetic overflow  |
       |-9999999999 | -2147483648 | printf: "-9999999999" arithmetic overflow |
       |ABC         | 0           | printf: "ABC" expected numeric value      |
       +------------+-------------+-------------------------------------------+
       The diagnostic message format is not specified, but these examples
       convey the type of information that should be reported. Note that the
       value shown on standard output is what would be expected as the return
       value from the _s_t_r_t_o_l() function as defined in the System Interfaces
       volume of POSIX.1-2017. A similar correspondence exists between %%uu and
       _s_t_r_t_o_u_l() and %%ee, %%ff, and %%gg (if the implementation supports floating-
       point conversions) and _s_t_r_t_o_d().

       In a locale using the ISO/IEC 646:1991 standard as the underlying
       codeset, the command:


           printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"

       produces:

       3     Numeric value of constant 3

       3     Numeric value of constant 3

       -3    Numeric value of constant -3

       51    Numeric value of the character ''33'' in the ISO/IEC 646:1991
             standard codeset

       43    Numeric value of the character ''++'' in the ISO/IEC 646:1991
             standard codeset

       45    Numeric value of the character ''--'' in the ISO/IEC 646:1991
             standard codeset

       Note that in a locale with multi-byte characters, the value of a
       character is intended to be the value of the equivalent of the wwcchhaarr__tt
       representation of the character as described in the System Interfaces
       volume of POSIX.1-2017.

RRAATTIIOONNAALLEE
       The _p_r_i_n_t_f utility was added to provide functionality that has
       historically been provided by _e_c_h_o.  However, due to irreconcilable
       differences in the various versions of _e_c_h_o extant, the version has few
       special features, leaving those to this new _p_r_i_n_t_f utility, which is
       based on one in the Ninth Edition system.

       The EXTENDED DESCRIPTION section almost exactly matches the _p_r_i_n_t_f()
       function in the ISO C standard, although it is described in terms of
       the file format notation in the Base Definitions volume of
       POSIX.1-2017, _C_h_a_p_t_e_r _5, _F_i_l_e _F_o_r_m_a_t _N_o_t_a_t_i_o_n.

       Earlier versions of this standard specified that arguments for all
       conversions other than bb, cc, and ss were evaluated in the same way (as C
       constants, but with stated exceptions). For implementations supporting
       the floating-point conversions it was not clear whether integer
       conversions need only accept integer constants and floating-point
       conversions need only accept floating-point constants, or whether both
       types of conversions should accept both types of constants. Also by not
       distinguishing between them, the requirement relating to a leading
       single-quote or double-quote applied to floating-point conversions even
       though this provided no useful functionality to applications that was
       not already available through the integer conversions. The current
       standard clarifies the situation by specifying that the arguments for
       floating-point conversions are evaluated as if by _s_t_r_t_o_d(), and the
       arguments for integer conversions are evaluated as C integer constants,
       with the special treatment of leading single-quote and double-quote
       applying only to integer conversions.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_w_k, _b_c, _e_c_h_o

       The Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _5, _F_i_l_e _F_o_r_m_a_t
       _N_o_t_a_t_i_o_n, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t _V_a_r_i_a_b_l_e_s

       The System Interfaces volume of POSIX.1-2017, _f_p_r_i_n_t_f(), _s_t_r_t_o_d()

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                            PRINTF(1)
