📄 snprintf.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>fprintf</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_008_804"> </a>NAME</h4><blockquote>fprintf, printf, snprintf, sprintf - print formatted output</blockquote><h4><a name = "tag_000_008_805"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="stdio.h.html">stdio.h</a>>int fprintf(FILE *<i>stream</i>, const char *<i>format</i>, ...);int printf(const char *<i>format</i>, ...);int snprintf(char *s, size_t <i>n</i>, const char *<i>format</i>, ...);int sprintf(char *s, const char *<i>format</i>, ...);</code></pre></blockquote><h4><a name = "tag_000_008_806"> </a>DESCRIPTION</h4><blockquote>The<i>fprintf()</i>function places output on the named output<i>stream</i>.The<i>printf()</i>function places output on the standard output stream<i>stdout</i>.The<i>sprintf()</i>function places output followed by the null byte, '\0',in consecutive bytes starting at<i>*s</i>;it is the user's responsibility to ensure thatenough space is available.<p><i>snprintf()</i>is identical to<i>sprintf()</i>with the addition of the<i>n</i>argument, which states the size of the buffer referred to by<i>s</i>.<p>Each of these functionsconverts, formats and prints its argumentsunder control of the<i>format</i>.The<i>format</i>is a character string, beginning and ending in its initial shift state, ifany. The<i>format</i>is composed of zero or more directives:<i>ordinary characters</i>,which are simply copied to the output stream and<i>conversion specifications</i>,each of which results in the fetching of zero or more arguments. The resultsare undefined if there are insufficient arguments for the<i>format</i>.If the<i>format</i>is exhausted whilearguments remain, the excessarguments are evaluated but are otherwise ignored.<p>Conversions can be applied to the<i>n</i>thargument after the<i>format</i>in the argument list, rather than to the next unused argument.In this case, the conversion character % (see below) is replacedby the sequence<i>%n$</i>where n is a decimal integer in the range [1, {NL_ARGMAX}],giving the position of the argument in the argument list.This feature provides for the definition of format strings thatselect arguments in an order appropriate to specific languages(see the EXAMPLES section).<p>In format stringscontaining the<i>%n$</i>form of conversion specifications, numberedarguments in the argument list can be referenced from the format stringas many times as required.<p>In format strings containing the % form of conversion specifications,each argument in the argument list is used exactly once.<p>All forms of the<i>fprintf()</i>functions allow for the insertion ofa language-dependent radix character in the output string.The radix character is definedin the program's locale (category LC_NUMERIC).In the POSIX locale, or in a locale wherethe radix character is not defined,the radix character defaults to a period (.).<p>Each conversion specification is introduced by the % characteror by the character sequence<i>%n$</i>,after which the following appear in sequence:<ul><li>Zero or more<i>flags</i>(in any order), which modify the meaning of the conversion specification.<p><li>An optional minimum<i>field width</i>.If the converted value has fewer bytes than the fieldwidth, it will be padded with spaces by defaulton the left; it will be padded on the right, if theleft-adjustment flag (-),described below, is given to the field width.The field width takes the form of an asterisk (*), describedbelow, or a decimal integer.<p><li>An optional<i>precision</i>that gives the minimum number of digits to appear for the d, i, o, u, xand X conversions; the number of digits to appear afterthe radix character for the e, E and fconversions; the maximum number of significant digits for the g and Gconversions; or the maximum number of bytes to be printed from a string in s and S conversions. The precision takes the form of a period (.) followed eitherby an asterisk (*), described below, or an optional decimal digit string,where a null digit string is treated as 0. If a precision appears with anyother conversion character, the behaviour is undefined.<p><li>An optional h specifying that a following d, i, o, u, x or Xconversion character applies to a type<b>short int</b>or type<b>unsigned short int</b>argument (the argument will have been promoted according to theintegral promotions, and its value will be converted to type<b>short int</b>or<b>unsigned short int</b>before printing); an optional hspecifying that a following nconversion character applies to a pointer to a type<b>short int</b>argument; an optional l (ell) specifying that a following d, i, o, u, x or Xconversion character applies to a type<b>long int</b>or<b>unsigned long int</b>argument; an optional l (ell) specifying that a following nconversion character applies to a pointer to a type<b>long int</b>argument; or an optional L specifying that a following e, E, f, g or Gconversion character applies to a type<b>long double</b>argument.If an h, l or L appears with any other conversion character, the behaviour isundefined.<br><p><li>An optional l specifying that a following c conversion characterapplies to a<b>wint_t</b>argument; an optional l specifying that a following s conversioncharacter applies to a pointer to a <b>wchar_t</b>argument.<p><li>A<i>conversion character</i>that indicates the type of conversion to be applied.<p></ul><p>A field width, or precision, or both, may be indicated by anasterisk (*).In this case an argument of type<b>int</b>supplies the field width or precision.Arguments specifying fieldwidth, or precision, or both must appear in that orderbefore the argument, if any, to be converted.A negative field width is taken as a - flag followed by apositive field width.A negative precision is taken as if the precision were omitted. In format strings containing the<i>%n$</i>form of a conversion specification, a field width or precision may beindicated by the sequence<i>*m$</i>,where<i>m</i>is a decimal integer in the range [1, {NL_ARGMAX}] givingthe position in the argument list (after the format argument) of an integerargument containing the field width or precision, for example:<pre><code>printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);</code></pre><p>The<i>format</i>can contain either numbered argument specifications(that is,<i>%n$</i>and<i>*m$</i>),or unnumbered argument specifications (that is, % and *),but normally not both.The only exception to this is that %%can be mixed with the<i>%n$</i>form.The results of mixing numbered and unnumbered argumentspecifications in a<i>format</i>string are undefined.When numbered argument specifications are used, specifying the<i>N</i>thargument requires that all the leading arguments,from the first to the (<i>N-1</i>)th, arespecified in the format string.<p>The flag characters and their meanings are:<dl compact><dt>'<dd>The integer portion of the result of a decimal conversion(%i, %d, %u, %f, %g or %G)will be formatted with thousands' grouping characters.For other conversions the behaviour is undefined.The non-monetary grouping character is used.<dt>-<dd>The result of the conversion will be left-justified within the field.The conversion will be right-justified if this flag is notspecified.<dt>+<dd>The result of a signedconversion will always begin with a sign (+ or -).The conversion will begin with a sign only when a negative valueis converted if this flag is not specified.<dt>space<dd>If the first character of a signed conversion is not a sign or ifa signed conversion results in no characters, a spacewill be prefixed to the result.This means that if the space and +flags both appear, the space flag will be ignored.<dt>#<dd>This flag specifies that the value is to be convertedto an alternative form.For o conversion, it increases the precision (if necessary) to forcethe first digit of the result to be 0.For x or X conversions, a non-zero result will have 0x (or 0X)prefixed to it.For e, E, f, g or Gconversions, the result will always contain a radix character,even if no digits follow the radix character.Without this flag, a radix character appearsin the result of these conversions only if a digitfollows it.For g and G conversions, trailing zeros will<i>not</i>be removed from the resultas they normally are.For other conversions, the behaviour is undefined.<dt>0<dd>For d, i, o, u, x, X, e, E, f, g and Gconversions, leading zeros (following any indication of sign orbase) are used to pad to the field width; no space padding isperformed.If the 0 and - flags both appear, the 0 flag will be ignored.For d, i, o, u, x and Xconversions, if a precision is specified, the 0flag will be ignored.If the 0 and ' flags both appear, the groupingcharacters are inserted before zero padding.For other conversions, the behaviour is undefined.</dl><p>The conversion characters and their meanings are:<dl compact><dt>d, i<dd>The <b>int</b> argument is converted to a signed decimalin the style <b>[</b>-<b>]</b><i>dddd</i>.The precision specifies the minimum number of digits to appear;if the value being converted can be represented in fewer digits,it will be expanded with leading zeros. The default precision is1. The result of converting 0 with an explicit precisionof 0 is no characters.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -