isposix

Un outil pour savoir si une commande est posix - retour accueil

git clone git://bebou.netlib.re/isposix
Log | Files | Refs | README |

printf.html (33333B)


      1 <!-- Copyright 2001-2024 IEEE and The Open Group, All Rights Reserved -->
      2 <!DOCTYPE HTML>
      3 <html lang="en">
      4 <head>
      5 <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.8.0">
      6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      7 <link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group rhtm tool v1.2.4 -->
      8 <!-- Copyright (c) 2001-2024 The Open Group, All Rights Reserved -->
      9 <title>printf</title>
     10 </head>
     11 <body bgcolor="white">
     12 <div class="NAVHEADER">
     13 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0">
     14 <tr class="nav">
     15 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/pr.html" accesskey="P">&lt;&lt;&lt;
     16 Previous</a></td>
     17 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td>
     18 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/prs.html" accesskey="N">Next &gt;&gt;&gt;</a></td>
     19 </tr>
     20 </table>
     21 <hr align="left" width="100%"></div>
     22 <script language="JavaScript" src="../jscript/codes.js"></script><basefont size="3">
     23 <center><font size="2">The Open Group Base Specifications Issue 8<br>
     24 IEEE Std 1003.1-2024<br>
     25 Copyright © 2001-2024 The IEEE and The Open Group</font></center>
     26 <hr size="2" noshade>
     27 <a name="top" id="top"></a> <a name="printf" id="printf"></a> <a name="tag_20_96" id="tag_20_96"></a><!-- printf -->
     28 <h4 class="mansect"><a name="tag_20_96_01" id="tag_20_96_01"></a>NAME</h4>
     29 <blockquote>printf — write formatted output</blockquote>
     30 <h4 class="mansect"><a name="tag_20_96_02" id="tag_20_96_02"></a>SYNOPSIS</h4>
     31 <blockquote class="synopsis">
     32 <p><code><tt>printf</tt> <i>format</i> <b>[</b><i>argument</i><tt>...</tt><b>]</b></code></p>
     33 </blockquote>
     34 <h4 class="mansect"><a name="tag_20_96_03" id="tag_20_96_03"></a>DESCRIPTION</h4>
     35 <blockquote>
     36 <p>The <i>printf</i> utility shall write formatted operands to the standard output. The <i>argument</i> operands shall be formatted
     37 under control of the <i>format</i> operand.</p>
     38 </blockquote>
     39 <h4 class="mansect"><a name="tag_20_96_04" id="tag_20_96_04"></a>OPTIONS</h4>
     40 <blockquote>
     41 <p>None.</p>
     42 </blockquote>
     43 <h4 class="mansect"><a name="tag_20_96_05" id="tag_20_96_05"></a>OPERANDS</h4>
     44 <blockquote>
     45 <p>The following operands shall be supported:</p>
     46 <dl compact>
     47 <dd></dd>
     48 <dt><i>format</i></dt>
     49 <dd>A character string describing the format to use to write the remaining operands. See the EXTENDED DESCRIPTION section.</dd>
     50 <dt><i>argument</i></dt>
     51 <dd>The values to be written to standard output, under the control of <i>format</i>. See the EXTENDED DESCRIPTION section.</dd>
     52 </dl>
     53 </blockquote>
     54 <h4 class="mansect"><a name="tag_20_96_06" id="tag_20_96_06"></a>STDIN</h4>
     55 <blockquote>
     56 <p>Not used.</p>
     57 </blockquote>
     58 <h4 class="mansect"><a name="tag_20_96_07" id="tag_20_96_07"></a>INPUT FILES</h4>
     59 <blockquote>
     60 <p>None.</p>
     61 </blockquote>
     62 <h4 class="mansect"><a name="tag_20_96_08" id="tag_20_96_08"></a>ENVIRONMENT VARIABLES</h4>
     63 <blockquote>
     64 <p>The following environment variables shall affect the execution of <i>printf</i>:</p>
     65 <dl compact>
     66 <dd></dd>
     67 <dt><i>LANG</i></dt>
     68 <dd>Provide a default value for the internationalization variables that are unset or null. (See XBD <a href=
     69 "../basedefs/V1_chap08.html#tag_08_02"><i>8.2 Internationalization Variables</i></a> the precedence of internationalization
     70 variables used to determine the values of locale categories.)</dd>
     71 <dt><i>LC_ALL</i></dt>
     72 <dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
     73 <dt><i>LC_CTYPE</i></dt>
     74 <dd>Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as
     75 opposed to multi-byte characters in arguments).</dd>
     76 <dt><i>LC_MESSAGES</i></dt>
     77 <dd><br>
     78 Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.</dd>
     79 <dt><i>LC_NUMERIC</i></dt>
     80 <dd><br>
     81 Determine the locale for numeric formatting. It shall affect the format of numbers written using the <tt>e</tt>, <tt>E</tt>,
     82 <tt>f</tt>, <tt>g</tt>, and <tt>G</tt> conversion specifier characters (if supported).</dd>
     83 <dt><i>NLSPATH</i></dt>
     84 <dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
     85 Determine the location of messages objects and message catalogs. <img src="../images/opt-end.gif" alt="[Option End]" border=
     86 "0"></dd>
     87 </dl>
     88 </blockquote>
     89 <h4 class="mansect"><a name="tag_20_96_09" id="tag_20_96_09"></a>ASYNCHRONOUS EVENTS</h4>
     90 <blockquote>
     91 <p>Default.</p>
     92 </blockquote>
     93 <h4 class="mansect"><a name="tag_20_96_10" id="tag_20_96_10"></a>STDOUT</h4>
     94 <blockquote>
     95 <p>See the EXTENDED DESCRIPTION section.</p>
     96 </blockquote>
     97 <h4 class="mansect"><a name="tag_20_96_11" id="tag_20_96_11"></a>STDERR</h4>
     98 <blockquote>
     99 <p>The standard error shall be used only for diagnostic messages.</p>
    100 </blockquote>
    101 <h4 class="mansect"><a name="tag_20_96_12" id="tag_20_96_12"></a>OUTPUT FILES</h4>
    102 <blockquote>
    103 <p>None.</p>
    104 </blockquote>
    105 <h4 class="mansect"><a name="tag_20_96_13" id="tag_20_96_13"></a>EXTENDED DESCRIPTION</h4>
    106 <blockquote>
    107 <p>The application shall ensure that the <i>format</i> operand is a character string, beginning and ending in its initial shift
    108 state, if any. The <i>format</i> operand shall be used as the format string described in XBD <a href=
    109 "../basedefs/V1_chap05.html#tag_05"><i>5. File Format Notation</i></a> with the following exceptions:</p>
    110 <ol>
    111 <li>
    112 <p>A &lt;space&gt; in the format string, in any context other than a flag of a conversion specification, shall be treated as an
    113 ordinary character that is copied to the output.</p>
    114 </li>
    115 <li>
    116 <p>A <tt>'Δ'</tt> character in the format string shall be treated as a <tt>'Δ'</tt> character, not as a &lt;space&gt;.</p>
    117 </li>
    118 <li>
    119 <p>In addition to the escape sequences shown in XBD <a href="../basedefs/V1_chap05.html#tag_05"><i>5. File Format Notation</i></a>
    120 (<tt>'\\'</tt>, <tt>'\a'</tt>, <tt>'\b'</tt>, <tt>'\f'</tt>, <tt>'\n'</tt>, <tt>'\r'</tt>, <tt>'\t'</tt>, <tt>'\v'</tt>),
    121 <tt>"\ddd"</tt>, where <i>ddd</i> is a one, two, or three-digit octal number, shall be written as a byte with the numeric value
    122 specified by the octal number.</p>
    123 </li>
    124 <li>
    125 <p>The implementation shall not precede or follow output from the <tt>d</tt> or <tt>u</tt> conversion specifiers with &lt;blank&gt;
    126 characters not specified by the <i>format</i> operand.</p>
    127 </li>
    128 <li>
    129 <p>The implementation shall not precede output from the <tt>o</tt> conversion specifier with zeros not specified by the
    130 <i>format</i> operand.</p>
    131 </li>
    132 <li>
    133 <p>The <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, and <tt>G</tt> conversion specifiers
    134 need not be supported.</p>
    135 </li>
    136 <li>
    137 <p>An additional conversion specifier character, <tt>b</tt>, shall be supported as follows. The argument shall be taken to be a
    138 string that can contain &lt;backslash&gt;-escape sequences. The following &lt;backslash&gt;-escape sequences shall be
    139 supported:</p>
    140 <ul>
    141 <li>
    142 <p>The escape sequences listed in XBD <a href="../basedefs/V1_chap05.html#tag_05"><i>5. File Format Notation</i></a>
    143 (<tt>'\\'</tt>, <tt>'\a'</tt>, <tt>'\b'</tt>, <tt>'\f'</tt>, <tt>'\n'</tt>, <tt>'\r'</tt>, <tt>'\t'</tt>, <tt>'\v'</tt>), which
    144 shall be converted to the characters they represent.</p>
    145 </li>
    146 <li>
    147 <p><tt>"\0ddd"</tt>, where <i>ddd</i> is a zero, one, two, or three-digit octal number that shall be converted to a byte with the
    148 numeric value specified by the octal number.</p>
    149 </li>
    150 <li>
    151 <p><tt>'\c'</tt>, which shall not be written and shall cause <i>printf</i> to ignore any remaining characters in the string operand
    152 containing it, any remaining string operands, and any additional characters in the <i>format</i> operand. If a precision is
    153 specified and the argument contains a <tt>'\c'</tt> after the point at which the number of bytes indicated by the precision
    154 specification have been written, it is unspecified whether the <tt>'\c'</tt> takes effect.</p>
    155 </li>
    156 </ul>
    157 <p>The interpretation of a &lt;backslash&gt; followed by any other sequence of characters is unspecified.</p>
    158 <p>Bytes from the converted string shall be written until the end of the string or the number of bytes indicated by the precision
    159 specification is reached. If the precision is omitted, it shall be taken to be infinite, so all bytes up to the end of the
    160 converted string shall be written.</p>
    161 </li>
    162 <li>
    163 <p>Conversions can be applied to the <i>n</i>th <i>argument</i> operand rather than to the next <i>argument</i> operand. In this
    164 case, the conversion specifier character <tt>'%'</tt> is replaced by the sequence <tt>"%<i>n</i><tt>$"</tt></tt>, where <i>n</i> is
    165 a decimal integer in the range [1,{NL_ARGMAX}], giving the <i>argument</i> operand number. This feature provides for the definition
    166 of format strings that select arguments in an order appropriate to specific languages.</p>
    167 <p>The format can contain either numbered argument conversion specifications (that is, ones beginning with
    168 <tt>"%<i>n</i><tt>$"</tt></tt>), or unnumbered argument conversion specifications, but not both. The only exception to this is that
    169 <tt>"%%"</tt> can be mixed with the <tt>"%<i>n</i><tt>$"</tt></tt> form. The results of mixing numbered and unnumbered argument
    170 specifications that consume an argument are unspecified.</p>
    171 </li>
    172 <li>
    173 <p>For each conversion specification that consumes an argument, an <i>argument</i> operand shall be evaluated and converted to the
    174 appropriate type for the conversion as specified below. The operand to be evaluated shall be determined as follows:</p>
    175 <ul>
    176 <li>
    177 <p>If the conversion specification begins with a <tt>"%<i>n</i><tt>$"</tt></tt> sequence, the <i>n</i>th <i>argument</i> operand
    178 shall be evaluated.</p>
    179 </li>
    180 <li>
    181 <p>Otherwise, the evaluated operand shall be the next <i>argument</i> operand after the one evaluated by the previous conversion
    182 specification that consumed an argument; if there is no such previous conversion specification the first <i>argument</i> operand
    183 shall be evaluated.</p>
    184 </li>
    185 </ul>
    186 <p>If the <i>format</i> operand contains no conversion specifications that consume an argument and there are <i>argument</i>
    187 operands present, the results are unspecified.</p>
    188 </li>
    189 <li>
    190 <p>The <i>format</i> operand shall be reused as often as necessary to satisfy the <i>argument</i> operands. If conversion
    191 specifications beginning with a <tt>"%<i>n</i><tt>$"</tt></tt> sequence are used, on format reuse the value of <i>n</i> shall refer
    192 to the <i>n</i>th <i>argument</i> operand following the highest numbered <i>argument</i> operand consumed by the previous use of
    193 the <i>format</i> operand.</p>
    194 </li>
    195 <li>
    196 <p>If an <i>argument</i> operand to be consumed by a conversion specification does not exist:</p>
    197 <ul>
    198 <li>
    199 <p>If it is a numbered argument conversion specification, <i>printf</i> should write a diagnostic message to standard error and
    200 exit with non-zero status, but may behave as for an unnumbered argument conversion specification.</p>
    201 </li>
    202 <li>
    203 <p>If it is an unnumbered argument conversion specification, any extra <tt>b</tt>, <tt>c</tt>, or <tt>s</tt> conversion specifiers
    204 shall be evaluated as if a null string argument were supplied and any other extra conversion specifiers shall be evaluated as if a
    205 zero argument were supplied.</p>
    206 </li>
    207 </ul>
    208 </li>
    209 <li>
    210 <p>If a character sequence in the <i>format</i> operand begins with a <tt>'%'</tt> character, but does not form a valid conversion
    211 specification, the behavior is unspecified.</p>
    212 </li>
    213 <li>
    214 <p>The argument to the <tt>c</tt> conversion specifier can be a string containing zero or more bytes. If it contains one or more
    215 bytes, the first byte shall be written and any additional bytes shall be ignored. If the argument is an empty string, it is
    216 unspecified whether nothing is written or a null byte is written.</p>
    217 </li>
    218 </ol>
    219 <p>The <i>argument</i> operands shall be treated as strings if the corresponding conversion specifier is <tt>b</tt>, <tt>c</tt>, or
    220 <tt>s</tt>, and shall be evaluated as if by the <a href="../functions/strtod.html"><i>strtod</i>()</a> function if the
    221 corresponding conversion specifier is <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, or
    222 <tt>G</tt>. Otherwise, they shall be evaluated as unsuffixed C integer constants, as described by the ISO&nbsp;C standard, with the
    223 following extensions:</p>
    224 <ul>
    225 <li>
    226 <p>A leading &lt;plus-sign&gt; or &lt;hyphen-minus&gt; shall be allowed.</p>
    227 </li>
    228 <li>
    229 <p>If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of
    230 the character following the single-quote or double-quote.</p>
    231 </li>
    232 <li>
    233 <p>Suffixed integer constants may be allowed.</p>
    234 </li>
    235 </ul>
    236 <p>If an <i>argument</i> operand cannot be completely converted into an internal value appropriate to the corresponding conversion
    237 specification, a diagnostic message shall be written to standard error and the utility shall not exit with a zero exit status, but
    238 shall continue processing any remaining operands and shall write the value accumulated at the time the error was detected to
    239 standard output.</p>
    240 <p>It shall not be considered an error if an <i>argument</i> operand is not completely used for a <tt>b</tt>, <tt>c</tt>, or
    241 <tt>s</tt> conversion.</p>
    242 </blockquote>
    243 <h4 class="mansect"><a name="tag_20_96_14" id="tag_20_96_14"></a>EXIT STATUS</h4>
    244 <blockquote>
    245 <p>The following exit values shall be returned:</p>
    246 <dl compact>
    247 <dd></dd>
    248 <dt>&nbsp;0</dt>
    249 <dd>Successful completion.</dd>
    250 <dt>&gt;0</dt>
    251 <dd>An error occurred.</dd>
    252 </dl>
    253 </blockquote>
    254 <h4 class="mansect"><a name="tag_20_96_15" id="tag_20_96_15"></a>CONSEQUENCES OF ERRORS</h4>
    255 <blockquote>
    256 <p>Default.</p>
    257 </blockquote>
    258 <hr>
    259 <div class="box"><em>The following sections are informative.</em></div>
    260 <h4 class="mansect"><a name="tag_20_96_16" id="tag_20_96_16"></a>APPLICATION USAGE</h4>
    261 <blockquote>
    262 <p>The floating-point formatting conversion specifications of <a href="../functions/printf.html"><i>printf</i>()</a> are not
    263 required because all arithmetic in the shell is integer arithmetic. The <a href="../utilities/awk.html"><i>awk</i></a> utility
    264 performs floating-point calculations and provides its own <b>printf</b> function. The <a href="../utilities/bc.html"><i>bc</i></a>
    265 utility can perform arbitrary-precision floating-point arithmetic, but does not provide extensive formatting capabilities. (This
    266 <i>printf</i> utility cannot really be used to format <a href="../utilities/bc.html"><i>bc</i></a> output; it does not support
    267 arbitrary precision.) Implementations are encouraged to support the floating-point conversions as an extension.</p>
    268 <p>Note that this <i>printf</i> utility, like the <a href="../functions/printf.html"><i>printf</i>()</a> function defined in the
    269 System Interfaces volume of POSIX.1-2024 on which it is based, makes no special provision for dealing with multi-byte characters
    270 when using the <tt>%c</tt> conversion specification or when a precision is specified in a <tt>%b</tt> or <tt>%s</tt> conversion
    271 specification. Applications should be extremely cautious using either of these features when there are multi-byte characters in the
    272 character set.</p>
    273 <p>No provision is made in this volume of POSIX.1-2024 which allows field widths and precisions to be specified as <tt>'*'</tt>
    274 since the <tt>'*'</tt> can be replaced directly in the <i>format</i> operand using shell variable substitution. Implementations can
    275 also provide this feature as an extension if they so choose.</p>
    276 <p>Hexadecimal character constants as defined in the ISO&nbsp;C standard are not recognized in the <i>format</i> operand because
    277 there is no consistent way to detect the end of the constant. Octal character constants are limited to, at most, three octal
    278 digits, but hexadecimal character constants are only terminated by a non-hex-digit character. In the ISO&nbsp;C standard, string
    279 literal concatenation can be used to terminate a constant and follow it with a hexadecimal character to be written. In the shell,
    280 similar concatenation can be done using <tt>$'...'</tt> so that the shell converts the hexadecimal sequence before it executes
    281 <i>printf</i>.</p>
    282 <p>The <tt>%b</tt> conversion specification is not part of the ISO&nbsp;C standard; it has been added here as a portable way to
    283 process &lt;backslash&gt;-escapes expanded in string operands as provided by the <a href="../utilities/echo.html"><i>echo</i></a>
    284 utility. See also the APPLICATION USAGE section of <a href="../utilities/echo.html#"><i>echo</i></a> for ways to use <i>printf</i>
    285 as a replacement for all of the traditional versions of the <a href="../utilities/echo.html"><i>echo</i></a> utility.</p>
    286 <p>If an argument cannot be parsed correctly for the corresponding conversion specification, the <i>printf</i> utility is required
    287 to report an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion shall be
    288 reported as errors.</p>
    289 <p>Unlike the <a href="../functions/printf.html"><i>printf</i>()</a> function, when numbered conversion specifications are used,
    290 specifying the <i>N</i>th argument does not require that all the leading arguments, from the first to the (<i>N-1</i>)th, are
    291 specified in the format string. For example, <tt>"%3$s %1$d\n"</tt> is an acceptable <i>format</i> operand which evaluates the
    292 first and third <i>argument</i> operands but not the second.</p>
    293 </blockquote>
    294 <h4 class="mansect"><a name="tag_20_96_17" id="tag_20_96_17"></a>EXAMPLES</h4>
    295 <blockquote>
    296 <p>To alert the user and then print and read a series of prompts:</p>
    297 <pre>
    298 <tt>printf "\aPlease fill in the following: \nName: "
    299 read name
    300 printf "Phone number: "
    301 read phone
    302 </tt></pre>
    303 <p>To read out a list of right and wrong answers from a file, calculate the percentage correctly, and print them out. The numbers
    304 are right-justified and separated by a single &lt;tab&gt;. The percentage is written to one decimal place of accuracy:</p>
    305 <pre>
    306 <tt>while read right wrong ; do
    307     percent=$(echo "scale=1;($right*100)/($right+$wrong)" | bc)
    308     printf "%2d right\t%2d wrong\t(%s%%)\n" \
    309         $right $wrong $percent
    310 done &lt; database_file
    311 </tt></pre>
    312 The command:
    313 <pre>
    314 <tt>printf "%5d%4d\n" 1 21 321 4321 54321
    315 </tt></pre>
    316 <p>produces:</p>
    317 <pre>
    318 <tt>    1  21
    319   3214321
    320 54321   0
    321 </tt></pre>
    322 <p>Note that the <i>format</i> operand is used three times to print all of the given strings and that a <tt>'0'</tt> was supplied
    323 by <i>printf</i> to satisfy the last <tt>%4d</tt> conversion specification.</p>
    324 <p>The command:</p>
    325 <pre>
    326 <tt>printf '%d\n' 10 010 0x10
    327 </tt></pre>
    328 <p>produces:<br></p>
    329 <center>
    330 <table border="1" cellpadding="3" align="center">
    331 <tr valign="top">
    332 <th align="center">
    333 <p class="tent"><b>Output Line</b></p>
    334 </th>
    335 <th align="center">
    336 <p class="tent"><b>Explanation</b></p>
    337 </th>
    338 </tr>
    339 <tr valign="top">
    340 <td align="left">
    341 <p class="tent">10</p>
    342 </td>
    343 <td align="left">
    344 <p class="tent">Decimal representation of the numeric value of decimal integer constant 10</p>
    345 </td>
    346 </tr>
    347 <tr valign="top">
    348 <td align="left">
    349 <p class="tent">8</p>
    350 </td>
    351 <td align="left">
    352 <p class="tent">Decimal representation of the numeric value of octal integer constant 010</p>
    353 </td>
    354 </tr>
    355 <tr valign="top">
    356 <td align="left">
    357 <p class="tent">16</p>
    358 </td>
    359 <td align="left">
    360 <p class="tent">Decimal representation of the numeric value of hexadecimal integer constant 0x10</p>
    361 </td>
    362 </tr>
    363 </table>
    364 </center>
    365 <p class="tent">If the implementation supports floating-point conversions, the command:</p>
    366 <pre>
    367 <tt>LC_ALL=C printf '%.2f\n' 10 010 0x10 10.1e2 010.1e2 0x10.1p2
    368 </tt></pre>
    369 <p class="tent">produces:<br></p>
    370 <center>
    371 <table border="1" cellpadding="3" align="center">
    372 <tr valign="top">
    373 <th align="center">
    374 <p class="tent"><b>Output Line</b></p>
    375 </th>
    376 <th align="center">
    377 <p class="tent"><b>Explanation</b></p>
    378 </th>
    379 </tr>
    380 <tr valign="top">
    381 <td align="left">
    382 <p class="tent">10.00</p>
    383 </td>
    384 <td align="left">
    385 <p class="tent">The string <tt>"10"</tt> interpreted as a decimal value, with 2 digits of precision</p>
    386 </td>
    387 </tr>
    388 <tr valign="top">
    389 <td align="left">
    390 <p class="tent">10.00</p>
    391 </td>
    392 <td align="left">
    393 <p class="tent">The string <tt>"010"</tt> interpreted as a decimal (not octal) value, with 2 digits of precision</p>
    394 </td>
    395 </tr>
    396 <tr valign="top">
    397 <td align="left">
    398 <p class="tent">16.00</p>
    399 </td>
    400 <td align="left">
    401 <p class="tent">The string <tt>"0x10"</tt> interpreted as a hexadecimal value, with 2 digits of precision</p>
    402 </td>
    403 </tr>
    404 <tr valign="top">
    405 <td align="left">
    406 <p class="tent">1010.00</p>
    407 </td>
    408 <td align="left">
    409 <p class="tent">The string <tt>"10.1e2"</tt> interpreted as a decimal floating-point value, with 2 digits of precision</p>
    410 </td>
    411 </tr>
    412 <tr valign="top">
    413 <td align="left">
    414 <p class="tent">1010.00</p>
    415 </td>
    416 <td align="left">
    417 <p class="tent">The string <tt>"010.1e2"</tt> interpreted as a decimal (not octal) floating-point value, with 2 digits of
    418 precision</p>
    419 </td>
    420 </tr>
    421 <tr valign="top">
    422 <td align="left">
    423 <p class="tent">64.25</p>
    424 </td>
    425 <td align="left">
    426 <p class="tent">The string <tt>"0x10.1p2"</tt> interpreted as a hexadecimal floating-point value, with 2 digits of precision</p>
    427 </td>
    428 </tr>
    429 </table>
    430 </center>
    431 <p class="tent">The <i>printf</i> utility is required to notify the user when conversion errors are detected while producing
    432 numeric output; thus, the following results would be expected on an implementation with 32-bit two's-complement integers when
    433 <tt>%d</tt> is specified as the <i>format</i> operand:<br></p>
    434 <center>
    435 <table border="1" cellpadding="3" align="center">
    436 <tr valign="top">
    437 <th align="center">
    438 <p class="tent"><b>Argument</b></p>
    439 </th>
    440 <th align="center">
    441 <p class="tent"><b>Standard Output</b></p>
    442 </th>
    443 <th align="center">
    444 <p class="tent"><b>Diagnostic Output</b></p>
    445 </th>
    446 </tr>
    447 <tr valign="top">
    448 <td align="left">
    449 <p class="tent">5a</p>
    450 </td>
    451 <td align="left">
    452 <p class="tent">5</p>
    453 </td>
    454 <td align="left">
    455 <p class="tent">printf: "5a" not completely converted</p>
    456 </td>
    457 </tr>
    458 <tr valign="top">
    459 <td align="left">
    460 <p class="tent">9999999999</p>
    461 </td>
    462 <td align="left">
    463 <p class="tent">2147483647</p>
    464 </td>
    465 <td align="left">
    466 <p class="tent">printf: "9999999999" arithmetic overflow</p>
    467 </td>
    468 </tr>
    469 <tr valign="top">
    470 <td align="left">
    471 <p class="tent">-9999999999</p>
    472 </td>
    473 <td align="left">
    474 <p class="tent">-2147483648</p>
    475 </td>
    476 <td align="left">
    477 <p class="tent">printf: "-9999999999" arithmetic overflow</p>
    478 </td>
    479 </tr>
    480 <tr valign="top">
    481 <td align="left">
    482 <p class="tent">ABC</p>
    483 </td>
    484 <td align="left">
    485 <p class="tent">0</p>
    486 </td>
    487 <td align="left">
    488 <p class="tent">printf: "ABC" expected numeric value</p>
    489 </td>
    490 </tr>
    491 </table>
    492 </center>
    493 <p class="tent">The diagnostic message format is not specified, but these examples convey the type of information that should be
    494 reported. Note that the value shown on standard output is what would be expected as the return value from the <a href=
    495 "../functions/strtol.html"><i>strtol</i>()</a> function as defined in the System Interfaces volume of POSIX.1-2024. A similar
    496 correspondence exists between <tt>%u</tt> and <a href="../functions/strtoul.html"><i>strtoul</i>()</a> and <tt>%e</tt>,
    497 <tt>%f</tt>, and <tt>%g</tt> (if the implementation supports floating-point conversions) and <a href=
    498 "../functions/strtod.html"><i>strtod</i>()</a>.</p>
    499 <p class="tent">In a locale that uses a codeset based on the ISO/IEC&nbsp;646:1991 standard, the command:</p>
    500 <pre>
    501 <tt>printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"
    502 </tt></pre>
    503 <p class="tent">produces:<br></p>
    504 <center>
    505 <table border="1" cellpadding="3" align="center">
    506 <tr valign="top">
    507 <th align="center">
    508 <p class="tent"><b>Output Line</b></p>
    509 </th>
    510 <th align="center">
    511 <p class="tent"><b>Explanation</b></p>
    512 </th>
    513 </tr>
    514 <tr valign="top">
    515 <td align="left">
    516 <p class="tent">3</p>
    517 </td>
    518 <td align="left">
    519 <p class="tent">Decimal representation of the numeric value 3</p>
    520 </td>
    521 </tr>
    522 <tr valign="top">
    523 <td align="left">
    524 <p class="tent">3</p>
    525 </td>
    526 <td align="left">
    527 <p class="tent">Decimal representation of the numeric value +3</p>
    528 </td>
    529 </tr>
    530 <tr valign="top">
    531 <td align="left">
    532 <p class="tent">-3</p>
    533 </td>
    534 <td align="left">
    535 <p class="tent">Decimal representation of the numeric value -3</p>
    536 </td>
    537 </tr>
    538 <tr valign="top">
    539 <td align="left">
    540 <p class="tent">51</p>
    541 </td>
    542 <td align="left">
    543 <p class="tent">Decimal representation of the numeric value of the character <tt>'3'</tt> in the ISO/IEC&nbsp;646:1991 standard
    544 codeset</p>
    545 </td>
    546 </tr>
    547 <tr valign="top">
    548 <td align="left">
    549 <p class="tent">43</p>
    550 </td>
    551 <td align="left">
    552 <p class="tent">Decimal representation of the numeric value of the character <tt>'+'</tt> in the ISO/IEC&nbsp;646:1991 standard
    553 codeset</p>
    554 </td>
    555 </tr>
    556 <tr valign="top">
    557 <td align="left">
    558 <p class="tent">45</p>
    559 </td>
    560 <td align="left">
    561 <p class="tent">Decimal representation of the numeric value of the character <tt>'-'</tt> in the ISO/IEC&nbsp;646:1991 standard
    562 codeset</p>
    563 </td>
    564 </tr>
    565 </table>
    566 </center>
    567 <p class="tent">Since the last two arguments contain more characters than used for the conversion, a diagnostic message is
    568 generated and the exit status is non-zero. Note that in a locale with multi-byte characters, the value of a character is intended
    569 to be the value of the equivalent of the <b>wchar_t</b> representation of the character as described in the System Interfaces
    570 volume of POSIX.1-2024.</p>
    571 </blockquote>
    572 <h4 class="mansect"><a name="tag_20_96_18" id="tag_20_96_18"></a>RATIONALE</h4>
    573 <blockquote>
    574 <p>The <i>printf</i> utility was added to provide functionality that has historically been provided by <a href=
    575 "../utilities/echo.html"><i>echo</i></a>. However, due to irreconcilable differences in the various versions of <a href=
    576 "../utilities/echo.html"><i>echo</i></a> extant, the version has few special features, leaving those to this new <i>printf</i>
    577 utility, which is based on one in the Ninth Edition system.</p>
    578 <p class="tent">The format strings for the <i>printf</i> utility are handled differently than for the <a href=
    579 "../functions/printf.html"><i>printf</i>()</a> function in several respects. Notable differences include:</p>
    580 <ul>
    581 <li class="tent">Reuse of the format until all arguments have been consumed.</li>
    582 <li class="tent">No provision for obtaining field width and precision from argument values.</li>
    583 <li class="tent">No requirement to support floating-point conversion specifiers.</li>
    584 <li class="tent">An additional <tt>b</tt> conversion specifier.</li>
    585 <li class="tent">Special handling of leading single-quote or double-quote for integer conversion specifiers.</li>
    586 <li class="tent">Hexadecimal character constants are not recognized in the format.</li>
    587 <li class="tent">Formats that use numbered argument conversion specifications can have gaps in the argument numbers.</li>
    588 </ul>
    589 <p class="tent">Although <i>printf</i> implementations have no difficulty handling formats with mixed numbered and unnumbered
    590 conversion specifications (unlike the <a href="../functions/printf.html"><i>printf</i>()</a> function where it is undefined
    591 behavior), existing implementations differ in behavior. Given the format operand <tt>"%2$d&nbsp;%d\n"</tt>, with some
    592 implementations the <tt>"%d"</tt> evaluates the first argument and with others it evaluates the third. Consequently this standard
    593 leaves the behavior unspecified (as opposed to undefined).</p>
    594 <p class="tent">Earlier versions of this standard specified that arguments for all conversions other than <tt>b</tt>, <tt>c</tt>,
    595 and <tt>s</tt> were evaluated in the same way (as C constants, but with stated exceptions). For implementations supporting the
    596 floating-point conversions it was not clear whether integer conversions need only accept integer constants and floating-point
    597 conversions need only accept floating-point constants, or whether both types of conversions should accept both types of constants.
    598 Also by not distinguishing between them, the requirement relating to a leading single-quote or double-quote applied to
    599 floating-point conversions even though this provided no useful functionality to applications that was not already available through
    600 the integer conversions. The current standard clarifies the situation by specifying that the arguments for floating-point
    601 conversions are evaluated as if by <a href="../functions/strtod.html"><i>strtod</i>()</a>, and the arguments for integer
    602 conversions are evaluated as C integer constants, with the special treatment of leading single-quote and double-quote applying only
    603 to integer conversions.</p>
    604 </blockquote>
    605 <h4 class="mansect"><a name="tag_20_96_19" id="tag_20_96_19"></a>FUTURE DIRECTIONS</h4>
    606 <blockquote>
    607 <p>A future version of this standard may require that a missing <i>argument</i> operand to be consumed by a numbered argument
    608 conversion specification is treated as an error.</p>
    609 <p class="tent">A future version of this standard is expected to add a <tt>%b</tt> conversion to the <a href=
    610 "../functions/printf.html"><i>printf</i>()</a> function for binary conversion of integers, in alignment with the next version of
    611 the ISO&nbsp;C standard. This will result in an inconsistency between the <i>printf</i> utility and <a href=
    612 "../functions/printf.html"><i>printf</i>()</a> function for format strings containing <tt>%b</tt>. Implementors are encouraged to
    613 collaborate on a way to address this which could then be adopted in a future version of this standard. For example, the
    614 <i>printf</i> utility could add a <b>-C</b> option to make the format string behave in the same way, as far as possible, as the
    615 <a href="../functions/printf.html"><i>printf</i>()</a> function.</p>
    616 <p class="tent">A future version of this standard may add a <tt>%q</tt> conversion to convert a string argument to a quoted output
    617 format that can be reused as shell input.</p>
    618 </blockquote>
    619 <h4 class="mansect"><a name="tag_20_96_20" id="tag_20_96_20"></a>SEE ALSO</h4>
    620 <blockquote>
    621 <p><a href="../utilities/awk.html#"><i>awk</i></a> , <a href="../utilities/bc.html#"><i>bc</i></a> , <a href=
    622 "../utilities/echo.html#"><i>echo</i></a></p>
    623 <p class="tent">XBD <a href="../basedefs/V1_chap05.html#tag_05"><i>5. File Format Notation</i></a> , <a href=
    624 "../basedefs/V1_chap08.html#tag_08"><i>8. Environment Variables</i></a></p>
    625 <p class="tent">XSH <a href="../functions/fprintf.html#"><i>fprintf</i></a> , <a href=
    626 "../functions/strtod.html#"><i>strtod</i></a></p>
    627 </blockquote>
    628 <h4 class="mansect"><a name="tag_20_96_21" id="tag_20_96_21"></a>CHANGE HISTORY</h4>
    629 <blockquote>
    630 <p>First released in Issue 4.</p>
    631 </blockquote>
    632 <h4 class="mansect"><a name="tag_20_96_22" id="tag_20_96_22"></a>Issue 7</h4>
    633 <blockquote>
    634 <p>Austin Group Interpretations 1003.1-2001 #175 and #177 are applied.</p>
    635 <p class="tent">SD5-XCU-ERN-97 is applied, updating the SYNOPSIS.</p>
    636 <p class="tent">POSIX.1-2008, Technical Corrigendum 2, XCU/TC2-2008/0156 [727], XCU/TC2-2008/0157 [727,932], XCU/TC2-2008/0158
    637 [584], and XCU/TC2-2008/0159 [727] are applied.</p>
    638 </blockquote>
    639 <h4 class="mansect"><a name="tag_20_96_23" id="tag_20_96_23"></a>Issue 8</h4>
    640 <blockquote>
    641 <p>Austin Group Defect 1108 is applied, changing &quot;twos&quot; to &quot;two's&quot;.</p>
    642 <p class="tent">Austin Group Defect 1122 is applied, changing the description of <i>NLSPATH .</i></p>
    643 <p class="tent">Austin Group Defect 1202 is applied, changing the description of how <tt>'\c'</tt> is handled by the <tt>b</tt>
    644 conversion specifier.</p>
    645 <p class="tent">Austin Group Defects 1209 and 1476 are applied, changing the EXAMPLES section.</p>
    646 <p class="tent">Austin Group Defect 1413 is applied, changing the APPLICATION USAGE section.</p>
    647 <p class="tent">Austin Group Defect 1562 is applied, clarifying that it is the application's responsibility to ensure that the
    648 format is a character string, beginning and ending in its initial shift state, if any.</p>
    649 <p class="tent">Austin Group Defect 1592 is applied, adding support for numbered conversion specifications.</p>
    650 <p class="tent">Austin Group Defect 1771 is applied, changing the FUTURE DIRECTIONS section.</p>
    651 </blockquote>
    652 <div class="box"><em>End of informative text.</em></div>
    653 <hr>
    654 <p>&nbsp;</p>
    655 <a href="#top"><span class="topOfPage">return to top of page</span></a><br>
    656 <hr size="2" noshade>
    657 <center><font size="2">UNIX® is a registered Trademark of The Open Group.<br>
    658 POSIX™ is a Trademark of The IEEE.<br>
    659 Copyright © 2001-2024 The IEEE and The Open Group, All Rights Reserved<br>
    660 [ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
    661 "../functions/contents.html">XSH</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../xrat/contents.html">XRAT</a>
    662 ]</font></center>
    663 <hr size="2" noshade>
    664 <div class="NAVHEADER">
    665 <table summary="Header navigation table" class="nav" width="100%" border="0" cellpadding="0" cellspacing="0">
    666 <tr class="nav">
    667 <td class="nav" width="15%" align="left" valign="bottom"><a href="../utilities/pr.html" accesskey="P">&lt;&lt;&lt;
    668 Previous</a></td>
    669 <td class="nav" width="70%" align="center" valign="bottom"><a href="contents.html">Home</a></td>
    670 <td class="nav" width="15%" align="right" valign="bottom"><a href="../utilities/prs.html" accesskey="N">Next &gt;&gt;&gt;</a></td>
    671 </tr>
    672 </table>
    673 <hr align="left" width="100%"></div>
    674 </body>
    675 </html>