📄 fprintf.html
字号:
<p>A <b>double</b> argument representing an infinity shall be converted in one of the styles <tt>"[-]inf"</tt> or<tt>"[-]infinity"</tt> ; which style is implementation-defined. A <b>double</b> argument representing a NaN shall be converted inone of the styles <tt>"[-]nan(</tt><i>n-char-sequence</i><tt>)"</tt> or <tt>"[-]nan"</tt> ; which style, and the meaning of any<i>n-char-sequence</i>, is implementation-defined. The <tt>F</tt> conversion specifier produces <tt>"INF"</tt>,<tt>"INFINITY"</tt>, or <tt>"NAN"</tt> instead of <tt>"inf"</tt>, <tt>"infinity"</tt>, or <tt>"nan"</tt>, respectively.</p></dd><dt><tt>e</tt>, <tt>E</tt></dt><dd>The <b>double</b> argument shall be converted in the style<tt>"[-]</tt><i>d</i><tt>.</tt><i>ddd</i><tt>e±</tt><i>dd</i><tt>"</tt>, where there is one digit before the radix character(which is non-zero if the argument is non-zero) and the number of digits after it is equal to the precision; if the precision ismissing, it shall be taken as 6; if the precision is zero and no <tt>'#'</tt> flag is present, no radix character shall appear. Thelow-order digit shall be rounded in an implementation-defined manner. The <tt>E</tt> conversion specifier shall produce a numberwith <tt>'E'</tt> instead of <tt>'e'</tt> introducing the exponent. The exponent shall always contain at least two digits. If thevalue is zero, the exponent shall be zero. <p>A <b>double</b> argument representing an infinity or NaN shall be converted in the style of an <tt>f</tt> or <tt>F</tt>conversion specifier.</p></dd><dt><tt>g</tt>, <tt>G</tt></dt><dd>The <b>double</b> argument shall be converted in the style <tt>f</tt> or <tt>e</tt> (or in the style <tt>F</tt> or <tt>E</tt>in the case of a <tt>G</tt> conversion specifier), with the precision specifying the number of significant digits. If an explicitprecision is zero, it shall be taken as 1. The style used depends on the value converted; style <tt>e</tt> (or <tt>E</tt> ) shallbe used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailingzeros shall be removed from the fractional portion of the result; a radix character shall appear only if it is followed by a digitor a <tt>'#'</tt> flag is present. <p>A <b>double</b> argument representing an infinity or NaN shall be converted in the style of an <tt>f</tt> or <tt>F</tt>conversion specifier.</p></dd><dt><tt>a</tt>, <tt>A</tt></dt><dd>A <b>double</b> argument representing a floating-point number shall be converted in the style<tt>"[-]0x</tt><i>h</i><tt>.</tt><i>hhhh</i><tt>p±</tt><i>d</i><tt>"</tt>, where there is one hexadecimal digit (which shallbe non-zero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point characterand the number of hexadecimal digits after it is equal to the precision; if the precision is missing and FLT_RADIX is a power of 2,then the precision shall be sufficient for an exact representation of the value; if the precision is missing and FLT_RADIX is not apower of 2, then the precision shall be sufficient to distinguish values of type <b>double</b>, except that trailing zeros may beomitted; if the precision is zero and the <tt>'#'</tt> flag is not specified, no decimal-point character shall appear. The letters<tt>"abcdef"</tt> shall be used for <tt>a</tt> conversion and the letters <tt>"ABCDEF"</tt> for <tt>A</tt> conversion. The<tt>A</tt> conversion specifier produces a number with <tt>'X'</tt> and <tt>'P'</tt> instead of <tt>'x'</tt> and <tt>'p'</tt>. Theexponent shall always contain at least one digit, and only as many more digits as necessary to represent the decimal exponent of 2.If the value is zero, the exponent shall be zero. <p>A <b>double</b> argument representing an infinity or NaN shall be converted in the style of an <tt>f</tt> or <tt>F</tt>conversion specifier.</p></dd><dt><tt>c</tt></dt><dd>The <b>int</b> argument shall be converted to an <b>unsigned char</b>, and the resulting byte shall be written. <p>If an <tt>l</tt> (ell) qualifier is present, the <b>wint_t</b> argument shall be converted as if by an <tt>ls</tt> conversionspecification with no precision and an argument that points to a two-element array of type <b>wchar_t</b>, the first element ofwhich contains the <b>wint_t</b> argument to the <tt>ls</tt> conversion specification and the second element contains a null widecharacter.</p></dd><dt><tt>s</tt></dt><dd>The argument shall be a pointer to an array of <b>char</b>. Bytes from the array shall be written up to (but not including) anyterminating null byte. If the precision is specified, no more than that many bytes shall be written. If the precision is notspecified or is greater than the size of the array, the application shall ensure that the array contains a null byte. <p>If an <tt>l</tt> (ell) qualifier is present, the argument shall be a pointer to an array of type <b>wchar_t</b>. Wide charactersfrom the array shall be converted to characters (each as if by a call to the <a href="../functions/wcrtomb.html"><i>wcrtomb</i>()</a> function, with the conversion state described by an <b>mbstate_t</b> objectinitialized to zero before the first wide character is converted) up to and including a terminating null wide character. Theresulting characters shall be written up to (but not including) the terminating null character (byte). If no precision isspecified, the application shall ensure that the array contains a null wide character. If a precision is specified, no more thanthat many characters (bytes) shall be written (including shift sequences, if any), and the array shall contain a null widecharacter if, to equal the character sequence length given by the precision, the function would need to access a wide character onepast the end of the array. In no case shall a partial character be written.</p></dd><dt><tt>p</tt></dt><dd>The argument shall be a pointer to <b>void</b>. The value of the pointer is converted to a sequence of printable characters, inan implementation-defined manner.</dd><dt><tt>n</tt></dt><dd>The argument shall be a pointer to an integer into which is written the number of bytes written to the output so far by thiscall to one of the <i>fprintf</i>() functions. No argument is converted.</dd><dt><tt>C</tt></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Equivalent to <tt>lc</tt>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><tt>S</tt></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Equivalent to <tt>ls</tt>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><tt>%</tt></dt><dd>Print a <tt>'%'</tt> character; no argument is converted. The complete conversion specification shall be <tt>%%</tt>.</dd></dl><p>If a conversion specification does not match one of the above forms, the behavior is undefined. If any argument is not thecorrect type for the corresponding conversion specification, the behavior is undefined.</p><p>In no case shall a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than thefield width, the field shall be expanded to contain the conversion result. Characters generated by <i>fprintf</i>() and<i>printf</i>() are printed as if <a href="../functions/fputc.html"><i>fputc</i>()</a> had been called.</p><p>For the <tt>a</tt> and <tt>A</tt> conversion specifiers, if FLT_RADIX is a power of 2, the value shall be correctly rounded to ahexadecimal floating number with the given precision.</p><p>For <tt>a</tt> and <tt>A</tt> conversions, if FLT_RADIX is not a power of 2 and the result is not exactly representable in thegiven precision, the result should be one of the two adjacent numbers in hexadecimal floating style with the given precision, withthe extra stipulation that the error should have a correct sign for the current rounding direction.</p><p>For the <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, and <tt>G</tt> conversion specifiers, if the number ofsignificant decimal digits is at most DECIMAL_DIG, then the result should be correctly rounded. If the number of significantdecimal digits is more than DECIMAL_DIG but the source value is exactly representable with DECIMAL_DIG digits, then the resultshould be an exact representation with trailing zeros. Otherwise, the source value is bounded by two adjacent decimal strings<i>L</i> < <i>U</i>, both having DECIMAL_DIG significant digits; the value of the resultant decimal string <i>D</i> shouldsatisfy <i>L</i> <= <i>D</i> <= <i>U</i>, with the extra stipulation that the error should have a correct sign for thecurrent rounding direction.</p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The<i>st_ctime</i> and <i>st_mtime</i> fields of the file shall be marked for update between the call to a successful execution of<i>fprintf</i>() or <i>printf</i>() and the next successful completion of a call to <a href="../functions/fflush.html"><i>fflush</i>()</a> or <a href="../functions/fclose.html"><i>fclose</i>()</a> on the same stream or acall to <a href="../functions/exit.html"><i>exit</i>()</a> or <a href="../functions/abort.html"><i>abort</i>()</a>. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_180_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, the <i>fprintf</i>() and <i>printf</i>() functions shall return the number of bytes transmitted.</p><p>Upon successful completion, the <i>sprintf</i>() function shall return the number of bytes written to <i>s</i>, excluding theterminating null byte.</p><p>Upon successful completion, the <i>snprintf</i>() function shall return the number of bytes that would be written to <i>s</i>had <i>n</i> been sufficiently large excluding the terminating null byte.</p><p>If an output error was encountered, these functions shall return a negative value.</p><p>If the value of <i>n</i> is zero on a call to <i>snprintf</i>(), nothing shall be written, the number of bytes that would havebeen written had <i>n</i> been sufficiently large excluding the terminating null shall be returned, and <i>s</i> may be a nullpointer.</p></blockquote><h4><a name="tag_03_180_05"></a>ERRORS</h4><blockquote><p>For the conditions under which <i>fprintf</i>() and <i>printf</i>() fail and may fail, refer to <a href="fputc.html"><i>fputc</i>()</a> or <a href="fputwc.html"><i>fputwc</i>()</a>.</p><p>In addition, all forms of <i>fprintf</i>() may fail if:</p><dl compact><dt>[EILSEQ]</dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">A wide-character code that does not correspond to a valid character has been detected. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt>[EINVAL]</dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">There are insufficient arguments. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl><p>The <i>printf</i>() and <i>fprintf</i>() functions may fail if:</p><dl compact><dt>[ENOMEM]</dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Insufficient storage space is available. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl><p>The <i>snprintf</i>() function shall fail if:</p><dl compact><dt>[EOVERFLOW]</dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The value of <i>n</i> is greater than {INT_MAX} or the number of bytes needed to hold the output excluding the terminating null isgreater than {INT_MAX}. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_180_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_180_06_01"></a>Printing Language-Independent Date and Time</h5><p>The following statement can be used to print date and time using a language-independent format:</p><pre><tt>printf(format, weekday, month, day, hour, min);</tt></pre><p>For American usage, <i>format</i> could be a pointer to the following string:</p><pre><tt>"%s, %s %d, %d:%.2d\n"</tt></pre><p>This example would produce the following message:</p><pre><tt>Sunday, July 3, 10:02</tt></pre><p>For German usage, <i>format</i> could be a pointer to the following string:</p><pre><tt>"%1$s, %3$d. %2$s, %4$d:%5$.2d\n"</tt></pre><p>This definition of <i>format</i> would produce the following message:</p><pre><tt>Sonntag, 3. Juli, 10:02</tt></pre><h5><a name="tag_03_180_06_02"></a>Printing File Information</h5><p>The following example prints information about the type, permissions, and number of links of a specific file in a directory.</p><p>The first two calls to <i>printf</i>() use data decoded from a previous <a href="../functions/stat.html"><i>stat</i>()</a> call.The user-defined <i>strperm</i>() function shall return a string similar to the one at the beginning of the output for thefollowing command:</p><pre><tt>ls -l</tt></pre><p>The next call to <i>printf</i>() outputs the owner's name if it is found using <a href="../functions/getpwuid.html"><i>getpwuid</i>()</a>; the <a href="../functions/getpwuid.html"><i>getpwuid</i>()</a> function shallreturn a <b>passwd</b> structure from which the name of the user is extracted. If the user name is not found, the program insteadprints out the numeric value of the user ID.</p><p>The next call prints out the group name if it is found using <a href="../functions/getgrgid.html"><i>getgrgid</i>()</a>; <ahref="../functions/getgrgid.html"><i>getgrgid</i>()</a> is very similar to <a href="../functions/getpwuid.html"><i>getpwuid</i>()</a> except that it shall return group information based on the group number. Onceagain, if the group is not found, the program prints the numeric value of the group for the entry.</p><p>The final call to <i>printf</i>() prints the size of the file.</p><pre><tt>#include <stdio.h>#include <sys/types.h>#include <pwd.h>#include <grp.h><br>char *strperm (mode_t);...struct stat statbuf;struct passwd *pwd;struct group *grp;...printf("%10.10s", strperm (statbuf.st_mode));printf("%4d", statbuf.st_nlink);<br>if ((pwd = getpwuid(statbuf.st_uid)) != NULL) printf(" %-8.8s", pwd->pw_name);else printf(" %-8ld", (long) statbuf.st_uid);<br>if ((grp = getgrgid(statbuf.st_gid)) != NULL) printf(" %-8.8s", grp->gr_name);else printf(" %-8ld", (long) statbuf.st_gid);<br>printf("%9jd", (intmax_t) statbuf.st_size);...</tt></pre><h5><a name="tag_03_180_06_03"></a>Printing a Localized Date String</h5><p>The following example gets a localized date string. The <a href="../functions/nl_langinfo.html"><i>nl_langinfo</i>()</a>function shall return the localized date string, which specifies the order and layout of the date. The <a href="../functions/strftime.html"><i>strftime</i>()</a> function takes this information and, using the <b>tm</b> structure for values,places the date and time information into <i>datestring</i>. The <i>printf</i>() function then outputs <i>datestring</i> and the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -