Files
oldlinux-files/Ref-docs/POSIX/susv3/utilities/printf.html
2024-02-19 00:21:47 -05:00

526 lines
19 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>printf</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="printf"></a> <a name="tag_04_102"></a><!-- printf -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_04_102_01"></a>NAME</h4>
<blockquote>printf - write formatted output</blockquote>
<h4><a name="tag_04_102_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>printf</tt> <i>format</i><b>[</b><i>argument</i><tt>...</tt><b>]</b></code></p>
</blockquote>
<h4><a name="tag_04_102_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>printf</i> utility shall write formatted operands to the standard output. The <i>argument</i> operands shall be formatted
under control of the <i>format</i> operand.</p>
</blockquote>
<h4><a name="tag_04_102_04"></a>OPTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_102_05"></a>OPERANDS</h4>
<blockquote>
<p>The following operands shall be supported:</p>
<dl compact>
<dt><i>format</i></dt>
<dd>A string describing the format to use to write the remaining operands. See the EXTENDED DESCRIPTION section.</dd>
<dt><i>argument</i></dt>
<dd>The strings to be written to standard output, under the control of <i>format</i>. See the EXTENDED DESCRIPTION section.</dd>
</dl>
</blockquote>
<h4><a name="tag_04_102_06"></a>STDIN</h4>
<blockquote>
<p>Not used.</p>
</blockquote>
<h4><a name="tag_04_102_07"></a>INPUT FILES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_102_08"></a>ENVIRONMENT VARIABLES</h4>
<blockquote>
<p>The following environment variables shall affect the execution of <i>printf</i>:</p>
<dl compact>
<dt><i>LANG</i></dt>
<dd>Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap08.html#tag_08_02">Section 8.2, Internationalization Variables</a> for
the precedence of internationalization variables used to determine the values of locale categories.)</dd>
<dt><i>LC_ALL</i></dt>
<dd>If set to a non-empty string value, override the values of all the other internationalization variables.</dd>
<dt><i>LC_CTYPE</i></dt>
<dd>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).</dd>
<dt><i>LC_MESSAGES</i></dt>
<dd>Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard
error.</dd>
<dt><i>LC_NUMERIC</i></dt>
<dd><br>
Determine the locale for numeric formatting. It shall affect the format of numbers written using the <tt>e</tt> , <tt>E</tt> ,
<tt>f</tt> , <tt>g</tt> , and <tt>G</tt> conversion specifier characters (if supported).</dd>
<dt><i>NLSPATH</i></dt>
<dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Determine the location of message catalogs for the processing of <i>LC_MESSAGES .</i> <img src="../images/opt-end.gif" alt=
"[Option End]" border="0"></dd>
</dl>
</blockquote>
<h4><a name="tag_04_102_09"></a>ASYNCHRONOUS EVENTS</h4>
<blockquote>
<p>Default.</p>
</blockquote>
<h4><a name="tag_04_102_10"></a>STDOUT</h4>
<blockquote>
<p>See the EXTENDED DESCRIPTION section.</p>
</blockquote>
<h4><a name="tag_04_102_11"></a>STDERR</h4>
<blockquote>
<p>The standard error shall be used only for diagnostic messages.</p>
</blockquote>
<h4><a name="tag_04_102_12"></a>OUTPUT FILES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_04_102_13"></a>EXTENDED DESCRIPTION</h4>
<blockquote>
<p>The <i>format</i> operand shall be used as the <i>format</i> string described in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/xbd_chap05.html">Chapter 5, File Format Notation</a> with the following
exceptions:</p>
<ol>
<li>
<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
ordinary character that is copied to the output.</p>
</li>
<li>
<p>A <tt>'<img src="../images/delta.gif" border="0">'</tt> character in the format string shall be treated as a <tt>'<img src=
"../images/delta.gif" border="0">'</tt> character, not as a &lt;space&gt;.</p>
</li>
<li>
<p>In addition to the escape sequences shown in the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap05.html">Chapter 5, File Format Notation</a> ( <tt>'\\'</tt> , <tt>'\a'</tt> , <tt>'\b'</tt> , <tt>'\f'</tt> ,
<tt>'\n'</tt> , <tt>'\r'</tt> , <tt>'\t'</tt> , <tt>'\v'</tt> ), <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 specified by the octal number.</p>
</li>
<li>
<p>The implementation shall not precede or follow output from the <tt>d</tt> or <tt>u</tt> conversion specifiers with
&lt;blank&gt;s not specified by the <i>format</i> operand.</p>
</li>
<li>
<p>The implementation shall not precede output from the <tt>o</tt> conversion specifier with zeros not specified by the
<i>format</i> operand.</p>
</li>
<li>
<p>The <tt>e</tt> , <tt>E</tt> , <tt>f</tt> , <tt>g</tt> , and <tt>G</tt> conversion specifiers need not be supported.</p>
</li>
<li>
<p>An additional conversion specifier character, <tt>b</tt> , shall be supported as follows. The argument shall be taken to be a
string that may contain backslash-escape sequences. The following backslash-escape sequences shall be supported:</p>
<ul>
<li>
<p>The escape sequences listed in the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/xbd_chap05.html">Chapter 5, File Format Notation</a> ( <tt>'\\'</tt> , <tt>'\a'</tt> , <tt>'\b'</tt> , <tt>'\f'</tt> ,
<tt>'\n'</tt> , <tt>'\r'</tt> , <tt>'\t'</tt> , <tt>'\v'</tt> ), which shall be converted to the characters they represent</p>
</li>
<li>
<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
numeric value specified by the octal number</p>
</li>
<li>
<p><tt>'\c'</tt> , which shall not be written and shall cause <i>printf</i> to ignore any remaining characters in the string
operand containing it, any remaining string operands, and any additional characters in the <i>format</i> operand</p>
</li>
</ul>
<p>The interpretation of a backslash followed by any other sequence of characters is unspecified.</p>
<p>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.</p>
</li>
<li>
<p>For each conversion specification that consumes an argument, the next argument operand shall be evaluated and converted to the
appropriate type for the conversion as specified below.</p>
</li>
<li>
<p>The <i>format</i> operand shall be reused as often as necessary to satisfy the argument operands. Any extra <tt>c</tt> or
<tt>s</tt> 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 <i>format</i> operand contains no conversion
specifications and <i>argument</i> operands are present, the results are unspecified.</p>
</li>
<li>
<p>If a character sequence in the <i>format</i> operand begins with a <tt>'%'</tt> character, but does not form a valid conversion
specification, the behavior is unspecified.</p>
</li>
</ol>
<p>The <i>argument</i> operands shall be treated as strings if the corresponding conversion specifier is <tt>b</tt> , <tt>c</tt> ,
or <tt>s</tt> ; otherwise, it shall be evaluated as a C constant, as described by the ISO&nbsp;C standard, with the following
extensions:</p>
<ul>
<li>
<p>A leading plus or minus sign shall be allowed.</p>
</li>
<li>
<p>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.</p>
</li>
</ul>
<p>If an argument 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.</p>
<p>It is not considered an error if an argument operand is not completely used for a <tt>c</tt> or <tt>s</tt> conversion or if a
string operand's first or second character is used to get the numeric value of a character.</p>
</blockquote>
<h4><a name="tag_04_102_14"></a>EXIT STATUS</h4>
<blockquote>
<p>The following exit values shall be returned:</p>
<dl compact>
<dt>&nbsp;0</dt>
<dd>Successful completion.</dd>
<dt>&gt;0</dt>
<dd>An error occurred.</dd>
</dl>
</blockquote>
<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 not
required because all arithmetic in the shell is integer arithmetic. The <a href="../utilities/awk.html"><i>awk</i></a> utility
performs 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 support
arbitrary 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 the
System Interfaces volume of IEEE&nbsp;Std&nbsp;1003.1-2001 on which it is based, makes no special provision for dealing with
multi-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 are
multi-byte characters in the character set.</p>
<p>No provision is made in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 which allows field widths and precisions to be specified
as <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&nbsp;C standard are not recognized in the <i>format</i> 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&nbsp;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&nbsp;C standard; it has been added here as a portable way to
process 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 for
all 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 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.</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 name
printf "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 numbers
are right-justified and separated by a single &lt;tab&gt;. 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 $percent
done &lt; 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
3214321
54321 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 supplied
by <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> is
specified 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>&nbsp;</b></p>
</th>
<th align="center">
<p class="tent"><b>Standard</b></p>
</th>
<th align="center">
<p class="tent"><b>&nbsp;</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. Note
that 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 of
IEEE&nbsp;Std&nbsp;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 supports
floating-point conversions) and <a href="../functions/strtod.html"><i>strtod</i>()</a>.</p>
<p>In a locale using the ISO/IEC&nbsp;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&nbsp;646:1991 standard codeset</dd>
<dt>43</dt>
<dd>Numeric value of the character <tt>'+'</tt> in the ISO/IEC&nbsp;646:1991 standard codeset</dd>
<dt>45</dt>
<dd>Numeric value of the character <tt>'-'</tt> in the ISO/IEC&nbsp;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&nbsp;Std&nbsp;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 in
the ISO&nbsp;C standard, although it is described in terms of the file format notation in the Base Definitions volume of
IEEE&nbsp;Std&nbsp;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 System
Interfaces volume of IEEE&nbsp;Std&nbsp;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>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>