⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fprintf.html

📁 posix标准英文,html格式
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!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-2004 IEEE and The Open Group, All Rights Reserved --><title>fprintf</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="fprintf"></a> <a name="tag_03_180"></a><!-- fprintf --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright &copy; 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_180_01"></a>NAME</h4><blockquote>fprintf, printf, snprintf, sprintf - print formatted output</blockquote><h4><a name="tag_03_180_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/stdio.h.html">stdio.h</a>&gt;<br><br> int fprintf(FILE *restrict</tt> <i>stream</i><tt>, const char *restrict</tt> <i>format</i><tt>, ...);<br> int printf(const char *restrict</tt> <i>format</i><tt>, ...);<br> int snprintf(char *restrict</tt> <i>s</i><tt>, size_t</tt> <i>n</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const char *restrict</tt> <i>format</i><tt>, ...);<br> int sprintf(char *restrict</tt> <i>s</i><tt>, const char *restrict</tt> <i>format</i><tt>, ...);<br></tt></code></p></blockquote><h4><a name="tag_03_180_03"></a>DESCRIPTION</h4><blockquote><div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"border="0"> The functionality described on this reference page is aligned with the ISO&nbsp;C standard. Any conflict between therequirements described here and the ISO&nbsp;C standard is unintentional. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 defers tothe ISO&nbsp;C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div><p>The <i>fprintf</i>() function shall place output on the named output <i>stream</i>. The <i>printf</i>() function shall placeoutput on the standard output stream <i>stdout</i>. The <i>sprintf</i>() function shall place output followed by the null byte,<tt>'\0'</tt>, in consecutive bytes starting at *<i>s</i>; it is the user's responsibility to ensure that enough space isavailable.</p><p>The <i>snprintf</i>() function shall be equivalent to <i>sprintf</i>(), with the addition of the <i>n</i> argument which statesthe size of the buffer referred to by <i>s</i>. If <i>n</i> is zero, nothing shall be written and <i>s</i> may be a null pointer.Otherwise, output bytes beyond the <i>n</i>-1st shall be discarded instead of being written to the array, and a null byte iswritten at the end of the bytes actually written into the array.</p><p>If copying takes place between objects that overlap as a result of a call to <i>sprintf</i>() or <i>snprintf</i>(), the resultsare undefined.</p><p>Each of these functions converts, formats, and prints its arguments under control of the <i>format</i>. The <i>format</i> is acharacter string, beginning and ending in its initial shift state, if any. The <i>format</i> is composed of zero or moredirectives: <i>ordinary characters</i>, which are simply copied to the output stream, and <i>conversion specifications</i>, each ofwhich shall result in the fetching of zero or more arguments. The results are undefined if there are insufficient arguments for the<i>format</i>. If the <i>format</i> is exhausted while arguments remain, the excess arguments shall be evaluated but are otherwiseignored.</p><p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">Conversions can be applied to the <i>n</i>th argument after the <i>format</i> in the argument list, rather than to the next unusedargument. In this case, the conversion specifier character <tt>%</tt> (see below) is replaced by the sequence<tt>"%</tt><i>n</i><tt>$"</tt>, where <i>n</i> is a decimal integer in the range [1,{NL_ARGMAX}], giving the position of theargument in the argument list. This feature provides for the definition of format strings that select arguments in an orderappropriate to specific languages (see the EXAMPLES section).</p><p>The <i>format</i> can contain either numbered argument conversion specifications (that is, <tt>"%</tt><i>n</i><tt>$"</tt> and<tt>"*</tt><i>m</i><tt>$"</tt>), or unnumbered argument conversion specifications (that is, <tt>%</tt> and <tt>*</tt> ), but notboth. The only exception to this is that <tt>%%</tt> can be mixed with the <tt>"%</tt><i>n</i><tt>$"</tt> form. The results ofmixing numbered and unnumbered argument specifications in a <i>format</i> string are undefined. When numbered argumentspecifications are used, specifying the <i>N</i>th argument requires that all the leading arguments, from the first to the(<i>N-1</i>)th, are specified in the format string.</p><p>In format strings containing the <tt>"%</tt><i>n</i><tt>$"</tt> form of conversion specification, numbered arguments in theargument list can be referenced from the format string as many times as required. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>In format strings containing the <tt>%</tt> form of conversion specification, each conversion specification uses the firstunused argument in the argument list.</p><p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> Allforms of the <i>fprintf</i>() functions allow for the insertion of a language-dependent radix character in the output string. Theradix character is defined in the program's locale (category <i>LC_NUMERIC ).</i> In the POSIX locale, or in a locale where theradix character is not defined, the radix character shall default to a period ( <tt>'.'</tt> ). <img src="../images/opt-end.gif"alt="[Option End]" border="0"></p><p>Each conversion specification is introduced by the <tt>'%'</tt> character <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;or by thecharacter sequence <tt>"%</tt><i>n</i><tt>$"</tt>, <img src="../images/opt-end.gif" alt="[Option End]" border="0"> after which thefollowing appear in sequence:</p><ul><li><p>Zero or more <i>flags</i> (in any order), which modify the meaning of the conversion specification.</p></li><li><p>An optional minimum <i>field width</i>. If the converted value has fewer bytes than the field width, it shall be padded withspaces by default on the left; it shall be padded on the right if the left-adjustment flag ( <tt>'-'</tt> ), described below, isgiven to the field width. The field width takes the form of an asterisk ( <tt>'*'</tt> ), described below, or a decimalinteger.</p></li><li><p>An optional <i>precision</i> that gives the minimum number of digits to appear for the <tt>d</tt>, <tt>i</tt>, <tt>o</tt>,<tt>u</tt>, <tt>x</tt>, and <tt>X</tt> conversion specifiers; the number of digits to appear after the radix character for the<tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, and <tt>F</tt> conversion specifiers; the maximum number ofsignificant digits for the <tt>g</tt> and <tt>G</tt> conversion specifiers; or the maximum number of bytes to be printed from astring in the <tt>s</tt> <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;and <tt>S</tt> <img src="../images/opt-end.gif" alt="[Option End]" border="0"> &nbsp;conversionspecifiers. The precision takes the form of a period ( <tt>'.'</tt> ) followed either by an asterisk ( <tt>'*'</tt> ), describedbelow, or an optional decimal digit string, where a null digit string is treated as zero. If a precision appears with any otherconversion specifier, the behavior is undefined.</p></li><li><p>An optional length modifier that specifies the size of the argument.</p></li><li><p>A <i>conversion specifier</i> character that indicates the type of conversion to be applied.</p></li></ul><p>A field width, or precision, or both, may be indicated by an asterisk ( <tt>'*'</tt> ). In this case an argument of type<b>int</b> supplies the field width or precision. Applications shall ensure that arguments specifying field width, or precision, orboth appear in that order before the argument, if any, to be converted. A negative field width is taken as a <tt>'-'</tt> flagfollowed by a positive field width. A negative precision is taken as if the precision were omitted. <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;In formatstrings containing the <tt>"%</tt><i>n</i><tt>$"</tt> form of a conversion specification, a field width or precision may beindicated by the sequence <tt>"*</tt><i>m</i><tt>$"</tt>, where <i>m</i> is a decimal integer in the range [1,{NL_ARGMAX}] givingthe position in the argument list (after the <i>format</i> argument) of an integer argument containing the field width orprecision, for example:</p><pre><tt>printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);<img src="../images/opt-end.gif" border="0"></tt></pre><p>The flag characters and their meanings are:</p><dl compact><dt><tt>'</tt></dt><dd><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">The integer portion of the result of a decimal conversion ( <tt>%i</tt>, <tt>%d</tt>, <tt>%u</tt>, <tt>%f</tt>, <tt>%F</tt>,<tt>%g</tt>, or <tt>%G</tt> ) shall be formatted with thousands' grouping characters. For other conversions the behavior isundefined. The non-monetary grouping character is used. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd><dt><tt>-</tt></dt><dd>The result of the conversion shall be left-justified within the field. The conversion is right-justified if this flag is notspecified.</dd><dt><tt>+</tt></dt><dd>The result of a signed conversion shall always begin with a sign ( <tt>'+'</tt> or <tt>'-'</tt> ). The conversion shall beginwith a sign only when a negative value is converted if this flag is not specified.</dd><dt>&lt;space&gt;</dt><dd>If the first character of a signed conversion is not a sign or if a signed conversion results in no characters, a &lt;space&gt;shall be prefixed to the result. This means that if the &lt;space&gt; and <tt>'+'</tt> flags both appear, the &lt;space&gt; flagshall be ignored.</dd><dt><tt>#</tt></dt><dd>Specifies that the value is to be converted to an alternative form. For <tt>o</tt> conversion, it increases the precision (ifnecessary) to force the first digit of the result to be zero. For <tt>x</tt> or <tt>X</tt> conversion specifiers, a non-zero resultshall have 0x (or 0X) prefixed to it. For <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, the result shall always contain a radix character, even if no digits follow the radixcharacter. Without this flag, a radix character appears in the result of these conversions only if a digit follows it. For<tt>g</tt> and <tt>G</tt> conversion specifiers, trailing zeros shall <i>not</i> be removed from the result as they normally are.For other conversion specifiers, the behavior is undefined.</dd><dt><tt>0</tt></dt><dd>For <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, <tt>X</tt>, <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, leading zeros (following any indicationof sign or base) are used to pad to the field width; no space padding is performed. If the <tt>'0'</tt> and <tt>'-'</tt> flags bothappear, the <tt>'0'</tt> flag is ignored. For <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, and <tt>X</tt>conversion specifiers, if a precision is specified, the <tt>'0'</tt> flag is ignored. <sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> &nbsp;If the<tt>'0'</tt> and <tt>'&quot;</tt> flags both appear, the grouping characters are inserted before zero padding. For other conversions,the behavior is undefined. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd></dl><p>The length modifiers and their meanings are:</p><dl compact><dt><tt>hh</tt></dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>signed char</b> or <b>unsigned char</b> argument (the argument will have been promoted according to the integerpromotions, but its value shall be converted to <b>signed char</b> or <b>unsigned char</b> before printing); or that a following<tt>n</tt> conversion specifier applies to a pointer to a <b>signed char</b> argument.</dd><dt><tt>h</tt></dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>short</b> or <b>unsigned short</b> argument (the argument will have been promoted according to the integerpromotions, but its value shall be converted to <b>short</b> or <b>unsigned short</b> before printing); or that a following<tt>n</tt> conversion specifier applies to a pointer to a <b>short</b> argument.</dd><dt><tt>l</tt>&nbsp;(ell)</dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>long</b> or <b>unsigned long</b> argument; that a following <tt>n</tt> conversion specifier applies to a pointer toa <b>long</b> argument; that a following <tt>c</tt> conversion specifier applies to a <b>wint_t</b> argument; that a following<tt>s</tt> conversion specifier applies to a pointer to a <b>wchar_t</b> argument; or has no effect on a following <tt>a</tt>,<tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, or <tt>G</tt> conversion specifier.</dd><dt><tt>ll</tt>&nbsp;(ell-ell)</dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>long long</b> or <b>unsigned long long</b> argument; or that a following <tt>n</tt> conversion specifier applies toa pointer to a <b>long long</b> argument.</dd><dt><tt>j</tt></dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to an <b>intmax_t</b> or <b>uintmax_t</b> argument; or that a following <tt>n</tt> conversion specifier applies to apointer to an <b>intmax_t</b> argument.</dd><dt><tt>z</tt></dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>size_t</b> or the corresponding signed integer type argument; or that a following <tt>n</tt> conversion specifierapplies to a pointer to a signed integer type corresponding to a <b>size_t</b> argument.</dd><dt><tt>t</tt></dt><dd>Specifies that a following <tt>d</tt>, <tt>i</tt>, <tt>o</tt>, <tt>u</tt>, <tt>x</tt>, or <tt>X</tt> conversion specifierapplies to a <b>ptrdiff_t</b> or the corresponding <b>unsigned</b> type argument; or that a following <tt>n</tt> conversionspecifier applies to a pointer to a <b>ptrdiff_t</b> argument.</dd><dt><tt>L</tt></dt><dd>Specifies that a following <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>F</tt>, <tt>g</tt>, or<tt>G</tt> conversion specifier applies to a <b>long double</b> argument.</dd></dl><p>If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined.</p><p>The conversion specifiers and their meanings are:</p><dl compact><dt><tt>d</tt>,&nbsp;<tt>i</tt></dt><dd>The <b>int</b> argument shall be converted to a signed decimal in the style <tt>"[-]</tt><i>dddd</i><tt>"</tt>. The precisionspecifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it shall beexpanded with leading zeros. The default precision is 1. The result of converting zero with an explicit precision of zero shall beno characters.</dd><dt><tt>o</tt></dt><dd>The <b>unsigned</b> argument shall be converted to unsigned octal format in the style <tt>"</tt><i>dddd</i><tt>"</tt>. Theprecision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, itshall be expanded with leading zeros. The default precision is 1. The result of converting zero with an explicit precision of zeroshall be no characters.</dd><dt><tt>u</tt></dt><dd>The <b>unsigned</b> argument shall be converted to unsigned decimal format in the style <tt>"</tt><i>dddd</i><tt>"</tt>. Theprecision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, itshall be expanded with leading zeros. The default precision is 1. The result of converting zero with an explicit precision of zeroshall be no characters.</dd><dt><tt>x</tt></dt><dd>The <b>unsigned</b> argument shall be converted to unsigned hexadecimal format in the style <tt>"</tt><i>dddd</i><tt>"</tt>;the letters <tt>"abcdef"</tt> are used. The precision specifies the minimum number of digits to appear; if the value beingconverted can be represented in fewer digits, it shall be expanded with leading zeros. The default precision is 1. The result ofconverting zero with an explicit precision of zero shall be no characters.</dd><dt><tt>X</tt></dt><dd>Equivalent to the <tt>x</tt> conversion specifier, except that letters <tt>"ABCDEF"</tt> are used instead of <tt>"abcdef"</tt>.</dd><dt><tt>f</tt>,&nbsp;<tt>F</tt></dt><dd>The <b>double</b> argument shall be converted to decimal notation in the style<tt>"[-]</tt><i>ddd</i><tt>.</tt><i>ddd</i><tt>"</tt>, where the number of digits after the radix character is equal to theprecision specification. If the precision is missing, it shall be taken as 6; if the precision is explicitly zero and no<tt>'#'</tt> flag is present, no radix character shall appear. If a radix character appears, at least one digit appears before it.The low-order digit shall be rounded in an implementation-defined manner. 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -