📄 printf.html
字号:
<li>If the leading character is a single- or double-quote, the valuewill be the numericvalue in the underlying codeset of the character following the single- ordouble-quote.<p></ul><p>If an argument operand cannot be completely converted into an internalvalue appropriate to the corresponding conversion specification, a diagnosticmessage will be written to standard error and the utilitywill not exit with a zero exit status,but will continue processing any remaining operands andwill write the value accumulated at the time the error wasdetected to standard output.</blockquote><h4><a name = "tag_001_014_1777"> </a>EXIT STATUS</h4><blockquote>The following exit values are returned:<dl compact><dt>0<dd>Successful completion.<dt>>0<dd>An error occurred.</dl></blockquote><h4><a name = "tag_001_014_1778"> </a>CONSEQUENCES OF ERRORS</h4><blockquote>Default.</blockquote><h4><a name = "tag_001_014_1779"> </a>APPLICATION USAGE</h4><blockquote>The floating-point formatting conversion specifications of<i><a href="../xsh/printf.html">printf()</a></i>are not requiredbecause all arithmetic in the shell is integer arithmetic.The<i><a href="awk.html">awk</a></i>utility performs floating-point calculations and provides its own<b>printf</b>function.The<i><a href="bc.html">bc</a></i>utility can perform arbitrary-precision floating-pointarithmetic, but does not provide extensive formatting capabilities.(This<i>printf</i>utility cannot really be used to format<i><a href="bc.html">bc</a></i>output; it does not support arbitrary precision.)Implementations are encouraged to supportthe floating-point conversions as an extension.<p>Note that this<i>printf</i>utility, like the <b>XSH</b> specification<i><a href="../xsh/printf.html">printf()</a></i>functionon which it is based, makes no special provision for dealing with multi-bytecharacters when using the<b>%c</b>conversion specification or when a precisionis specified in a<b>%b</b>or<b>%s</b>conversion specification.Applicationsshould be extremely cautious using either of these features when thereare multi-byte characters in the character set.<p>Field widths and precisions cannot be specified as "*" since the "*"can be replaced directly in the<i>format</i>operand using shell variable substitution.Implementations can also provide this feature as an extension ifthey so choose.<p>Hexadecimal character constants as defined in the ISO C standard are notrecognised in the<i>format</i>operand because there is no consistent way todetect the end of the constant.Octal character constants are limitedto, at most, three octal digits, but hexadecimal character constants are onlyterminated by a non-hex-digit character.In the ISO C standard, the##concatenation operator can be used to terminate a constant and follow itwith a hexadecimal character to be written.In the shell, concatenation occursbefore the<i>printf</i>utility has a chance to parse the end of the hexadecimal constant.<p>The<b>%b</b>conversion specification is not part of the ISO C standard; it hasbeen added here as a portable way to process backslash escapes expandedin string operands as provided by the<i><a href="echo.html">echo</a></i>utility.See also the APPLICATION USAGE section of<i><a href="echo.html">echo</a></i>for ways to use<i>printf</i>as a replacement for all of the traditional versions of the<i><a href="echo.html">echo</a></i>utility.<p>If an argument cannot be parsed correctly for the corresponding conversionspecification, the<i>printf</i>utility is required to report an error.Thus, overflow and extraneous characters at the end of an argument beingused for a numeric conversion are to be reported as errors.<p>It is not considered an error if an argumentoperand is not completely used for acorsconversion or if a stringoperand's first or second character is used to get the numeric value of acharacter.</blockquote><h4><a name = "tag_001_014_1780"> </a>EXAMPLES</h4><blockquote>To alert the user and then print and read a series of prompts:<pre><code>printf "\aPlease fill in the following: \nName: "read nameprintf "Phone number: "read phone</code></pre><p>To read out a list of right and wrong answers from a file, calculatethe percentage correctly, and print them out.The numbers are right-justified and separated by a singletab character.The percentage is written to one decimal place of accuracy:<pre><code>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</code></pre>The command:<pre><code>printf "%5d%4d\n" 1 21 321 4321 54321</code></pre>produces:<pre><code> 1 21 321432154321 0</code></pre><p>Note that the<i>format</i>operand is used three times to print all of thegiven strings and that a0was supplied by<i>printf</i>to satisfy the last<b>%4d</b>conversion specification.<p>The<i>printf</i>utility is required to notify the user when conversionerrors are detected while producing numeric output; thus, thefollowing results would be expected on an implementation with32-bit twos-complement integers when<b>%d</b>is specified as the<i>format</i>operand:<pre><table bordercolor=#000000 border=1 align=center><tr valign=top><th align=center><b>Argument</b><th align=center><b>Standard<br>Output</b><th align=center><b>Diagnostic Output</b><tr valign=top><td align=left>5a<td align=left>5<td align=left>printf: "5a" not completely converted<tr valign=top><td align=left>9999999999<td align=left>2147483647<td align=left>printf: "9999999999" arithmetic overflow<tr valign=top><td align=left>-9999999999<td align=left>-2147483648<td align=left>printf: "-9999999999" arithmetic overflow<tr valign=top><td align=left>ABC<td align=left>0<td align=left>printf: "ABC" expected numeric value</table></pre><p>The diagnostic message format is not specified, but theseexamples convey the type of information that should be reported.Note that the value shown on standard output is what would beexpected as the return value from the <b>XSH</b> specificationfunction<i><a href="../xsh/strtol.html">strtol()</a></i>.A similar correspondence exists between<b>%u</b>and<i><a href="../xsh/strtoul.html">strtoul()</a></i>and<b>%e</b>,<b>%f</b>and<b>%g</b>(if the implementation supports floating-point conversions) and<i><a href="../xsh/strtod.html">strtod()</a></i>.<p>In a locale using the ISO/IEC 646:1991 standardas the underlying codeset, the command:<pre><code>printf "%d\n" 3 +3 -3 \'3 \"+3 "'-3"</code></pre>produces:<pre><table bordercolor=#000000 border=1 align=center><tr valign=top><td align=left>3<td align=left>Numeric value of constant 3<tr valign=top><td align=left>3<td align=left>Numeric value of constant 3<tr valign=top><td align=left>-3<td align=left>Numeric value of constant -3<tr valign=top><td align=left>51<td align=left>Numeric value of the character `3' in the ISO/IEC 646:1991 standard codeset<tr valign=top><td align=left>43<td align=left>Numeric value of the character `+' in the ISO/IEC 646:1991 standard codeset<tr valign=top><td align=left>45<td align=left>Numeric value of the character `-' in the ISO/IEC 646:1991 standard codeset</table></pre><p>Note that in a locale with multi-byte characters, the value of acharacter is intended to be the value of the equivalent of the<b>wchar_t</b>representation of the character as described in the <b>XSH</b> specification.</blockquote><h4><a name = "tag_001_014_1781"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_001_014_1782"> </a>SEE ALSO</h4><blockquote><i><a href="awk.html">awk</a></i>,<i><a href="bc.html">bc</a></i>,<i><a href="echo.html">echo</a></i>,the <b>XSH</b> specification description of<i><a href="../xsh/printf.html">printf()</a></i>.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -