📄 printf.html
字号:
<h4><a name="tag_04_102_15"></a>CONSEQUENCES OF ERRORS</h4><blockquote><p>Default.</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_04_102_16"></a>APPLICATION USAGE</h4><blockquote><p>The floating-point formatting conversion specifications of <a href="../functions/printf.html"><i>printf</i>()</a> are notrequired because all arithmetic in the shell is integer arithmetic. The <a href="../utilities/awk.html"><i>awk</i></a> utilityperforms floating-point calculations and provides its own <b>printf</b> function. The <a href="../utilities/bc.html"><i>bc</i></a>utility can perform arbitrary-precision floating-point arithmetic, but does not provide extensive formatting capabilities. (This<i>printf</i> utility cannot really be used to format <a href="../utilities/bc.html"><i>bc</i></a> output; it does not supportarbitrary precision.) Implementations are encouraged to support the floating-point conversions as an extension.</p><p>Note that this <i>printf</i> utility, like the <a href="../functions/printf.html"><i>printf</i>()</a> function defined in theSystem Interfaces volume of IEEE Std 1003.1-2001 on which it is based, makes no special provision for dealing withmulti-byte characters when using the <tt>%c</tt> conversion specification or when a precision is specified in a <tt>%b</tt> or<tt>%s</tt> conversion specification. Applications should be extremely cautious using either of these features when there aremulti-byte characters in the character set.</p><p>No provision is made in this volume of IEEE Std 1003.1-2001 which allows field widths and precisions to be specifiedas <tt>'*'</tt> since the <tt>'*'</tt> can be replaced directly in the <i>format</i> operand using shell variable substitution.Implementations can also provide this feature as an extension if they so choose.</p><p>Hexadecimal character constants as defined in the ISO C standard are not recognized in the <i>format</i> operand becausethere is no consistent way to detect the end of the constant. Octal character constants are limited to, at most, three octaldigits, but hexadecimal character constants are only terminated by a non-hex-digit character. In the ISO C standard, the<tt>"##"</tt> 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 <i>printf</i> utility has a chance to parse the end of the hexadecimal constant.</p><p>The <tt>%b</tt> conversion specification is not part of the ISO C standard; it has been added here as a portable way toprocess backslash escapes expanded in string operands as provided by the <a href="../utilities/echo.html"><i>echo</i></a> utility.See also the APPLICATION USAGE section of <a href="echo.html"><i>echo</i></a> for ways to use <i>printf</i> as a replacement forall of the traditional versions of the <a href="../utilities/echo.html"><i>echo</i></a> utility.</p><p>If an argument cannot be parsed correctly for the corresponding conversion specification, the <i>printf</i> utility is requiredto report an error. Thus, overflow and extraneous characters at the end of an argument being used for a numeric conversion shall bereported as errors.</p></blockquote><h4><a name="tag_04_102_17"></a>EXAMPLES</h4><blockquote><p>To alert the user and then print and read a series of prompts:</p><pre><tt>printf "\aPlease fill in the following: \nName: "read nameprintf "Phone number: "read phone</tt></pre><p>To read out a list of right and wrong answers from a file, calculate the percentage correctly, and print them out. The numbersare right-justified and separated by a single <tab>. The percentage is written to one decimal place of accuracy:</p><pre><tt>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 $percentdone < database_file</tt></pre>The command: <pre><tt>printf "%5d%4d\n" 1 21 321 4321 54321</tt></pre><p>produces:</p><pre><tt> 1 21 321432154321 0</tt></pre><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 suppliedby <i>printf</i> to satisfy the last <tt>%4d</tt> conversion specification.</p><p>The <i>printf</i> 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 <tt>%d</tt> isspecified as the <i>format</i> operand:</p><center><table border="1" cellpadding="3" align="center"><tr valign="top"><th align="center"><p class="tent"><b> </b></p></th><th align="center"><p class="tent"><b>Standard</b></p></th><th align="center"><p class="tent"><b> </b></p></th></tr><tr valign="top"><th align="center"><p class="tent"><b>Argument</b></p></th><th align="center"><p class="tent"><b>Output</b></p></th><th align="center"><p class="tent"><b>Diagnostic Output</b></p></th></tr><tr valign="top"><td align="left"><p class="tent">5a</p></td><td align="left"><p class="tent">5</p></td><td align="left"><p class="tent">printf: "5a" not completely converted</p></td></tr><tr valign="top"><td align="left"><p class="tent">9999999999</p></td><td align="left"><p class="tent">2147483647</p></td><td align="left"><p class="tent">printf: "9999999999" arithmetic overflow</p></td></tr><tr valign="top"><td align="left"><p class="tent">-9999999999</p></td><td align="left"><p class="tent">-2147483648</p></td><td align="left"><p class="tent">printf: "-9999999999" arithmetic overflow</p></td></tr><tr valign="top"><td align="left"><p class="tent">ABC</p></td><td align="left"><p class="tent">0</p></td><td align="left"><p class="tent">printf: "ABC" expected numeric value</p></td></tr></table></center><p>The diagnostic message format is not specified, but these examples convey the type of information that should be reported. Notethat the value shown on standard output is what would be expected as the return value from the <a href="../functions/strtol.html"><i>strtol</i>()</a> function as defined in the System Interfaces volume ofIEEE Std 1003.1-2001. A similar correspondence exists between <tt>%u</tt> and <a href="../functions/strtoul.html"><i>strtoul</i>()</a> and <tt>%e</tt>, <tt>%f</tt>, and <tt>%g</tt> (if the implementation supportsfloating-point conversions) and <a href="../functions/strtod.html"><i>strtod</i>()</a>.</p><p>In a locale using the ISO/IEC 646:1991 standard as the underlying codeset, the command:</p><pre><tt>printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"</tt></pre><p>produces:</p><dl compact><dt>3</dt><dd>Numeric value of constant 3</dd><dt>3</dt><dd>Numeric value of constant 3</dd><dt>-3</dt><dd>Numeric value of constant -3</dd><dt>51</dt><dd>Numeric value of the character <tt>'3'</tt> in the ISO/IEC 646:1991 standard codeset</dd><dt>43</dt><dd>Numeric value of the character <tt>'+'</tt> in the ISO/IEC 646:1991 standard codeset</dd><dt>45</dt><dd>Numeric value of the character <tt>'-'</tt> in the ISO/IEC 646:1991 standard codeset</dd></dl><p>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<b>wchar_t</b> representation of the character as described in the System Interfaces volume of IEEE Std 1003.1-2001.</p></blockquote><h4><a name="tag_04_102_18"></a>RATIONALE</h4><blockquote><p>The <i>printf</i> utility was added to provide functionality that has historically been provided by <a href="../utilities/echo.html"><i>echo</i></a>. However, due to irreconcilable differences in the various versions of <a href="../utilities/echo.html"><i>echo</i></a> extant, the version has few special features, leaving those to this new <i>printf</i>utility, which is based on one in the Ninth Edition system.</p><p>The EXTENDED DESCRIPTION section almost exactly matches the <a href="../functions/printf.html"><i>printf</i>()</a> function inthe ISO C standard, although it is described in terms of the file format notation in the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap05.html">Chapter 5, File Format Notation</a>.</p></blockquote><h4><a name="tag_04_102_19"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_04_102_20"></a>SEE ALSO</h4><blockquote><p><a href="awk.html"><i>awk</i></a>, <a href="bc.html"><i>bc</i></a>, <a href="echo.html"><i>echo</i></a>, the SystemInterfaces volume of IEEE Std 1003.1-2001, <a href="../functions/printf.html"><i>printf</i>()</a></p></blockquote><h4><a name="tag_04_102_21"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 4.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -