diff options
Diffstat (limited to 'devdocs/c/io%2Ffprintf.html')
| -rw-r--r-- | devdocs/c/io%2Ffprintf.html | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/devdocs/c/io%2Ffprintf.html b/devdocs/c/io%2Ffprintf.html new file mode 100644 index 00000000..c0309821 --- /dev/null +++ b/devdocs/c/io%2Ffprintf.html @@ -0,0 +1,278 @@ + <h1 id="firstHeading" class="firstHeading">printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdio.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(1)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">int printf( const char *format, ... );</pre> +</td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int printf( const char *restrict format, ... );</pre> +</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(2)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">int fprintf( FILE *stream, const char *format, ... );</pre> +</td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int fprintf( FILE *restrict stream, const char *restrict format, ... );</pre> +</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl-rev-aux"> <td></td> <td rowspan="3">(3)</td> <td></td> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">int sprintf( char *buffer, const char *format, ... );</pre> +</td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int sprintf( char *restrict buffer, const char *restrict format, ... );</pre> +</td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">int snprintf( char *restrict buffer, size_t bufsz, + const char *restrict format, ... );</pre> +</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int printf_s( const char *restrict format, ... );</pre> +</td> <td> (5) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int fprintf_s( FILE *restrict stream, const char *restrict format, ... );</pre> +</td> <td> (6) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int sprintf_s( char *restrict buffer, rsize_t bufsz, + const char *restrict format, ... );</pre> +</td> <td> (7) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">int snprintf_s( char *restrict buffer, rsize_t bufsz, + const char *restrict format, ... );</pre> +</td> <td> (8) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Loads the data from the given locations, converts them to character string equivalents and writes the results to a variety of sinks/streams:</p> +<div class="t-li1"> +<span class="t-li">1)</span> Writes the results to the output stream <code><a href="std_streams" title="c/io/std streams">stdout</a></code>.</div> <div class="t-li1"> +<span class="t-li">2)</span> Writes the results to the output stream <code>stream</code>.</div> <div class="t-li1"> +<span class="t-li">3)</span> Writes the results to a character string <code>buffer</code>. The behavior is undefined if the string to be written (plus the terminating null character) exceeds the size of the array pointed to by <code>buffer</code>.</div> <div class="t-li1"> +<span class="t-li">4)</span> Writes the results to a character string <code>buffer</code>. At most <code>bufsz</code> - 1 characters are written. The resulting character string will be terminated with a null character, unless <code>bufsz</code> is zero. If <code>bufsz</code> is zero, nothing is written and <code>buffer</code> may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.</div> <div class="t-li1"> +<span class="t-li">5-8)</span> Same as <span class="t-v">(1-4)</span>, except that the following errors are detected at runtime and call the currently installed <a href="../error/set_constraint_handler_s" title="c/error/set constraint handler s">constraint handler</a> function: <dl> +<dd> +<ul> +<li> the conversion specifier <code>%n</code> is present in <code>format</code> </li> +<li> any of the arguments corresponding to <code>%s</code> is a null pointer </li> +<li> <code>stream</code> or <code>format</code> or <code>buffer</code> is a null pointer </li> +<li> <code>bufsz</code> is zero or greater than <code>RSIZE_MAX</code> </li> +<li> encoding errors occur in any of string and character conversion specifiers </li> +<li> (for <code>sprintf_s</code> only), the string to be stored in <code>buffer</code> (including the trailing null) would exceed <code>bufsz</code> </li> +</ul> </dd> +<dd>As with all bounds-checked functions, <code>printf_s</code> , <code>fprintf_s</code>, <code>sprintf_s</code>, and <code>snprintf_s</code> are only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../io" title="c/io"><code><stdio.h></code></a>.</dd> +</dl> +</div> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> stream </td> <td> - </td> <td> output file stream to write to </td> +</tr> <tr class="t-par"> <td> buffer </td> <td> - </td> <td> pointer to a character string to write to </td> +</tr> <tr class="t-par"> <td> bufsz </td> <td> - </td> <td> up to bufsz - 1 characters may be written, plus the null terminator </td> +</tr> <tr class="t-par"> <td> format </td> <td> - </td> <td> pointer to a null-terminated byte string specifying how to interpret the data </td> +</tr> <tr class="t-par"> <td> ... </td> <td> - </td> <td> arguments specifying data to print. If any argument after <a href="../language/conversion#Default_argument_promotions" title="c/language/conversion">default argument promotions</a> is not the type expected by the corresponding conversion specifier, or if there are fewer arguments than required by <code>format</code>, the behavior is undefined. If there are more arguments than required by <code>format</code>, the extraneous arguments are evaluated and ignored. </td> +</tr> +</table> <p><br> The <b>format</b> string consists of ordinary byte characters (except <code>%</code>), which are copied unchanged into the output stream, and conversion specifications. Each conversion specification has the following format:</p> +<ul> +<li> introductory <code>%</code> character. </li> +<li> <span class="t-mark">(optional)</span> one or more flags that modify the behavior of the conversion: </li> +<ul> +<li> <code>-</code>: the result of the conversion is left-justified within the field (by default it is right-justified). </li> +<li> <code>+</code>: the sign of signed conversions is always prepended to the result of the conversion (by default the result is preceded by minus only when it is negative). </li> +<li> <i>space</i>: if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if <code>+</code> flag is present. </li> +<li> <code>#</code>: <i>alternative form</i> of the conversion is performed. See the table below for exact effects otherwise the behavior is undefined. </li> +<li> <code>0</code>: for integer and floating point number conversions, leading zeros are used to pad the field instead of <i>space</i> characters. For integer numbers it is ignored if the precision is explicitly specified. For other conversions using this flag results in undefined behavior. It is ignored if <code>-</code> flag is present. </li> +</ul> +<li> <span class="t-mark">(optional)</span> integer value or <code>*</code> that specifies minimum field width. The result is padded with <i>space</i> characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when <code>*</code> is used, the width is specified by an additional argument of type <code>int</code>, which appears before the argument to be converted and the argument supplying precision if one is supplied. If the value of the argument is negative, it results with the <code>-</code> flag specified and positive field width (Note: This is the minimum width: The value is never truncated.). </li> +<ul> +<li> <span class="t-mark">(optional)</span> <code>.</code> followed by integer number or <code>*</code>, or neither that specifies <i>precision</i> of the conversion. In the case when <code>*</code> is used, the <i>precision</i> is specified by an additional argument of type <code>int</code>, which appears before the argument to be converted, but after the argument supplying minimum field width if one is supplied. If the value of this argument is negative, it is ignored. If neither a number nor <code>*</code> is used, the precision is taken as zero. See the table below for exact effects of <i>precision</i>. </li> +<li> <span class="t-mark">(optional)</span> <i>length modifier</i> that specifies the size of the argument (in combination with the conversion format specifier, it specifies the type of the corresponding argument). </li> +<li> conversion format specifier. </li> +</ul> +</ul> <p>The following format specifiers are available:</p> +<table class="wikitable"> <tr> <th rowspan="1">Conversion<br>Specifier </th> <th rowspan="1">Explanation </th> <th colspan="9">Expected<br>Argument Type </th> +</tr> <tr> <th colspan="2"> +<b>Length<br>Modifier</b><b>→</b> </th> <th> +<code>hh</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <th> +<code>h</code> </th> <th>(none) </th> <th> +<code>l</code> </th> <th> +<code>ll</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <th> +<code>j</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <th> +<code>z</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <th> +<code>t</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <th> +<code>L</code> </th> +</tr> <tr> <th> +<code>%</code> </th> <td>Writes literal <code>%</code>. The full conversion specification must be <code>%%</code>. </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>c</code> </th> <td> Writes a <b>single character</b>. <p>The argument is first converted to <code>unsigned char</code>. If the <b>l</b> modifier is used, the argument is first converted to a character string as if by <b>%ls</b> with a <code>wchar_t[2]</code> argument.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td> +<div><code>int</code></div> </td> <td> +<div><code>wint_t</code></div> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>s</code> </th> <td> Writes a <b>character string</b> <p>The argument must be a pointer to the initial element of an array of characters. <i>Precision</i> specifies the maximum number of bytes to be written. If <i>Precision</i> is not specified, writes every byte up to and not including the first null terminator. If the <b>l</b> specifier is used, the argument must be a pointer to the initial element of an array of <code>wchar_t</code>, which is converted to char array as if by a call to <code><a href="../string/multibyte/wcrtomb" title="c/string/multibyte/wcrtomb">wcrtomb</a></code> with zero-initialized conversion state.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td> +<div><code>char*</code></div> </td> <td> +<div><code>wchar_t*</code></div> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>d</code><br><code>i</code> </th> <td> Converts a <b>signed integer</b> into decimal representation <i>[-]dddd</i>. <p><i>Precision</i> specifies the minimum number of digits to appear. The default precision is <code>1</code>.<br><br> If both the converted value and the precision are <code>0</code> the conversion results in no characters.<br><br></p> +</td> <td rowspan="1"> +<div><code>signed char</code></div> </td> <td rowspan="1"> +<div><code>short</code></div> </td> <td rowspan="1"> +<div><code>int</code></div> </td> <td rowspan="1"> +<div><code>long</code></div> </td> <td rowspan="1"> +<div><code>long long</code></div> </td> <td rowspan="1"> +<div><code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw118">intmax_t</span></a></code></div> </td> <td rowspan="1"> +<div>signed <code><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a></code> +</div> </td> <td rowspan="1"> +<div><code><a href="http://en.cppreference.com/w/c/types/ptrdiff_t"><span class="kw101">ptrdiff_t</span></a></code></div> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>o</code> </th> <td> Converts an <b>unsigned integer</b> into octal representation <i>oooo</i>. <p><i>Precision</i> specifies the minimum number of digits to appear. The default precision is <code>1</code>. If both the converted value and the precision are <code>0</code> the conversion results in no characters. In the <i>alternative implementation</i> precision is increased if necessary, to write one leading zero. In that case if both the converted value and the precision are <code>0</code>, single <code>0</code> is written.</p> +</td> <td rowspan="3"> +<div><code>unsigned char</code></div> </td> <td rowspan="3"> +<div><code>unsigned short</code></div> </td> <td rowspan="3"> +<div><code>unsigned int</code></div> </td> <td rowspan="3"> +<div><code>unsigned long</code></div> </td> <td rowspan="3"> +<div><code>unsigned long long</code></div> </td> <td rowspan="3"> +<div><code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw132">uintmax_t</span></a></code></div> </td> <td rowspan="3"> +<div><code><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a></code></div> </td> <td rowspan="3"> +<div>unsigned version of <code><a href="http://en.cppreference.com/w/c/types/ptrdiff_t"><span class="kw101">ptrdiff_t</span></a></code> +</div> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>x</code><br><code>X</code> </th> <td> Converts an <b>unsigned integer</b> into hexadecimal representation <i>hhhh</i>. <p>For the <code>x</code> conversion letters <code>abcdef</code> are used.<br> For the <code>X</code> conversion letters <code>ABCDEF</code> are used.<br> <i>Precision</i> specifies the minimum number of digits to appear. The default precision is <code>1</code>. If both the converted value and the precision are <code>0</code> the conversion results in no characters. In the <i>alternative implementation</i> <code>0x</code> or <code>0X</code> is prefixed to results if the converted value is nonzero.</p> +</td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>u</code> </th> <td> Converts an <b>unsigned integer</b> into decimal representation <i>dddd</i>. <p><i>Precision</i> specifies the minimum number of digits to appear. The default precision is <code>1</code>. If both the converted value and the precision are <code>0</code> the conversion results in no characters.</p> +</td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>f</code><br><code>F</code> </th> <td> Converts <b>floating-point number</b> to the decimal notation in the style <i>[-]ddd.ddd</i>. <p><i>Precision</i> specifies the exact number of digits to appear after the decimal point character. The default precision is <code>6</code>. In the <i>alternative implementation</i> decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td rowspan="4"> +<div><code>double</code></div> </td> <td rowspan="4"> +<div> +<code>double</code><span class="t-mark-rev t-since-c99">(C99)</span> +</div> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td rowspan="4"> +<div><code>long double</code></div> </td> +</tr> <tr> <th> +<code>e</code><br><code>E</code> </th> <td> Converts <b>floating-point number</b> to the decimal exponent notation. <p>For the <code>e</code> conversion style <i>[-]d.ddd</i><code>e</code><i>±dd</i> is used.<br> For the <code>E</code> conversion style <i>[-]d.ddd</i><code>E</code><i>±dd</i> is used.<br> The exponent contains at least two digits, more digits are used only if necessary. If the value is <code>0</code>, the exponent is also <code>0</code>. <i>Precision</i> specifies the exact number of digits to appear after the decimal point character. The default precision is <code>6</code>. In the <i>alternative implementation</i> decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>a</code><br><code>A</code> <p><span class="t-mark-rev t-since-c99">(C99)</span></p> +</th> <td> Converts <b>floating-point number</b> to the hexadecimal exponent notation. <p>For the <code>a</code> conversion style <i>[-]</i><code>0x</code><i>h.hhh</i><code>p</code><i>±d</i> is used.<br> For the <code>A</code> conversion style <i>[-]</i><code>0X</code><i>h.hhh</i><code>P</code><i>±d</i> is used.<br> The first hexadecimal digit is not <code>0</code> if the argument is a normalized floating point value. If the value is <code>0</code>, the exponent is also <code>0</code>. <i>Precision</i> specifies the exact number of digits to appear after the hexadecimal point character. The default precision is sufficient for exact representation of the value. In the <i>alternative implementation</i> decimal point character is written even if no digits follow it. For infinity and not-a-number conversion style see notes.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>g</code><br><code>G</code> </th> <td> Converts <b>floating-point number</b> to decimal or decimal exponent notation depending on the value and the <i>precision</i>. <p>For the <code>g</code> conversion style conversion with style <code>e</code> or <code>f</code> will be performed.<br> For the <code>G</code> conversion style conversion with style <code>E</code> or <code>F</code> will be performed.<br> Let <code>P</code> equal the precision if nonzero, <code>6</code> if the precision is not specified, or <code>1</code> if the precision is <code>0</code>. Then, if a conversion with style <code>E</code> would have an exponent of <code>X</code>:</p> +<ul> +<li> if <i>P > X ≥ −4</i>, the conversion is with style <code>f</code> or <code>F</code> and precision <i>P − 1 − X</i>. </li> +<li> otherwise, the conversion is with style <code>e</code> or <code>E</code> and precision <i>P − 1</i>. </li> +</ul> <p>Unless <i>alternative representation</i> is requested the trailing zeros are removed, also the decimal point character is removed if no fractional part is left. For infinity and not-a-number conversion style see notes.</p> +</td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>n</code> </th> <td> Returns the <b>number of characters written</b> so far by this call to the function. <p>The result is <i>written</i> to the value pointed to by the argument. The specification may not contain any <i>flag</i>, <i>field width</i>, or <i>precision</i>.<br><br><br><br></p> +</td> <td> +<div><code>signed char*</code></div> </td> <td> +<div><code>short*</code></div> </td> <td> +<div><code>int*</code></div> </td> <td> +<div><code>long*</code></div> </td> <td> +<div><code>long long*</code></div> </td> <td> +<div><code><a href="http://en.cppreference.com/w/c/types/integer"><span class="kw118">intmax_t</span></a><span class="sy2">*</span></code></div> </td> <td> +<div>signed <code><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="sy2">*</span></code> +</div> </td> <td> +<div><code><a href="http://en.cppreference.com/w/c/types/ptrdiff_t"><span class="kw101">ptrdiff_t</span></a><span class="sy2">*</span></code></div> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> <tr> <th> +<code>p</code> </th> <td>Writes an implementation defined character sequence defining a <b>pointer</b>. </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td> +<code>void*</code> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> <td class="table-na"> <small>N/A</small> </td> +</tr> +</table> <p>The floating point conversion functions convert infinity to <code>inf</code> or <code>infinity</code>. Which one is used is implementation defined.</p> +<p>Not-a-number is converted to <code>nan</code> or <code>nan(<i>char_sequence</i>)</code>. Which one is used is implementation defined.</p> +<p>The conversions <code>F</code>, <code>E</code>, <code>G</code>, <code>A</code> output <code>INF</code>, <code>INFINITY</code>, <code>NAN</code> instead.</p> +<p>Even though <code>%c</code> expects <code>int</code> argument, it is safe to pass a <code>char</code> because of the integer promotion that takes place when a variadic function is called.</p> +<p>The correct conversion specifications for the fixed-width character types (<code><a href="../types/integer" title="c/types/integer">int8_t</a></code>, etc) are defined in the header <a href="../types/integer" title="c/types/integer"><code><inttypes.h></code></a> (although <code><a href="../types/integer" title="c/types/integer">PRIdMAX</a></code>, <code><a href="../types/integer" title="c/types/integer">PRIuMAX</a></code>, etc is synonymous with <code>%jd</code>, <code>%ju</code>, etc).</p> +<p>The memory-writing conversion specifier <code>%n</code> is a common target of security exploits where format strings depend on user input and is not supported by the bounds-checked <code>printf_s</code> family of functions.</p> +<p>There is a <a href="../language/eval_order" title="c/language/eval order">sequence point</a> after the action of each conversion specifier; this permits storing multiple <code>%n</code> results in the same variable or, as an edge case, printing a string modified by an earlier <code>%n</code> within the same call.</p> +<p>If a conversion specification is invalid, the behavior is undefined.</p> +<h3 id="Return_value"> Return value</h3> <div class="t-li1"> +<span class="t-li">1,2)</span> number of characters transmitted to the output stream or negative value if an output error or an encoding error (for string and character conversion specifiers) occurred</div> <div class="t-li1"> +<span class="t-li">3)</span> number of characters written to <code>buffer</code> (not counting the terminating null character), or a negative value if an encoding error (for string and character conversion specifiers) occurred</div> <div class="t-li1"> +<span class="t-li">4)</span> number of characters (not including the terminating null character) which would have been written to <code>buffer</code> if <code>bufsz</code> was ignored, or a negative value if an encoding error (for string and character conversion specifiers) occurred</div> <div class="t-li1"> +<span class="t-li">5,6)</span> number of characters transmitted to the output stream or negative value if an output error, a runtime constraints violation error, or an encoding error occurred.</div> <div class="t-li1"> +<span class="t-li">7)</span> number of characters written to <code>buffer</code>, not counting the null character (which is always written as long as <code>buffer</code> is not a null pointer and <code>bufsz</code> is not zero and not greater than <code>RSIZE_MAX</code>), or zero on runtime constraint violations, and negative value on encoding errors</div> <div class="t-li1"> +<span class="t-li">8)</span> number of characters not including the terminating null character (which is always written as long as <code>buffer</code> is not a null pointer and <code>bufsz</code> is not zero and not greater than <code>RSIZE_MAX</code>), which would have been written to <code>buffer</code> if <code>bufsz</code> was ignored, or a negative value if a runtime constraints violation or an encoding error occurred</div> <h3 id="Notes"> Notes</h3> <p>The C standard and <a rel="nofollow" class="external text" href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html">POSIX</a> specify that the behavior of <code>sprintf</code> and its variants is undefined when an argument overlaps with the destination buffer. Example:</p> +<div class="c source-c"><pre data-language="c">sprintf(dst, "%s and %s", dst, t); // <- broken: undefined behavior</pre></div> <p><a rel="nofollow" class="external text" href="https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html">POSIX specifies</a> that <code><a href="../error/errno" title="c/error/errno">errno</a></code> is set on error. It also specifies additional conversion specifications, most notably support for argument reordering (<code>n$</code> immediately after <code>%</code> indicates <code>n</code><sup>th</sup> argument).</p> +<p>Calling <code>snprintf</code> with zero <code>bufsz</code> and null pointer for <code>buffer</code> is useful to determine the necessary buffer size to contain the output:</p> +<div class="c source-c"><pre data-language="c">const char fmt[] = "sqrt(2) = %f"; +int sz = snprintf(NULL, 0, fmt, sqrt(2)); +char buf[sz + 1]; // note +1 for terminating null byte +snprintf(buf, sizeof buf, fmt, sqrt(2));</pre></div> <p><code>snprintf_s</code>, just like <code>snprintf</code>, but unlike <code>sprintf_s</code>, will truncate the output to fit in <code>bufsz-1</code>.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +int main(void) +{ + const char* s = "Hello"; + printf("Strings:\n"); // same as puts("Strings"); + printf(" padding:\n"); + printf("\t[%10s]\n", s); + printf("\t[%-10s]\n", s); + printf("\t[%*s]\n", 10, s); + printf(" truncating:\n"); + printf("\t%.4s\n", s); + printf("\t%.*s\n", 3, s); + + printf("Characters:\t%c %%\n", 'A'); + + printf("Integers:\n"); + printf("\tDecimal:\t%i %d %.6i %i %.0i %+i %i\n", + 1, 2, 3, 0, 0, 4,-4); + printf("\tHexadecimal:\t%x %x %X %#x\n", 5, 10, 10, 6); + printf("\tOctal:\t\t%o %#o %#o\n", 10, 10, 4); + + printf("Floating point:\n"); + printf("\tRounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); + printf("\tPadding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); + printf("\tScientific:\t%E %e\n", 1.5, 1.5); + printf("\tHexadecimal:\t%a %A\n", 1.5, 1.5); + printf("\tSpecial values:\t0/0=%g 1/0=%g\n", 0.0/0.0, 1.0/0.0); + + printf("Fixed-width types:\n"); + printf("\tLargest 32-bit value is %" PRIu32 " or %#" PRIx32 "\n", + UINT32_MAX, UINT32_MAX ); +}</pre></div> <p>Possible output:</p> +<div class="text source-text"><pre data-language="c">Strings: + padding: + [ Hello] + [Hello ] + [ Hello] + truncating: + Hell + Hel +Characters: A % +Integers: + Decimal: 1 2 000003 0 +4 -4 + Hexadecimal: 5 a A 0x6 + Octal: 12 012 04 +Floating point: + Rounding: 1.500000 2 1.30000000000000004440892098500626 + Padding: 01.50 1.50 1.50 + Scientific: 1.500000E+00 1.500000e+00 + Hexadecimal: 0x1.8p+0 0X1.8P+0 + Special values: 0/0=-nan 1/0=inf +Fixed-width types: + Largest 32-bit value is 4294967295 or 0xffffffff</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul> +<li> 7.21.6.1 The fprintf function (p: 225-230) </li> +<li> 7.21.6.3 The printf function (p: 236) </li> +<li> 7.21.6.5 The snprintf function (p: 237) </li> +<li> 7.21.6.6 The sprintf function (p: 237) </li> +<li> K.3.5.3.1 The fprintf_s function (p: 430) </li> +<li> K.3.5.3.3 The printf_s function (p: 432) </li> +<li> K.3.5.3.5 The snprintf_s function (p: 432-433) </li> +<li> K.3.5.3.6 The sprintf_s function (p: 433) </li> +</ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul> +<li> 7.21.6.1 The fprintf function (p: 309-316) </li> +<li> 7.21.6.3 The printf function (p: 324) </li> +<li> 7.21.6.5 The snprintf function (p: 325) </li> +<li> 7.21.6.6 The sprintf function (p: 325-326) </li> +<li> K.3.5.3.1 The fprintf_s function (p: 591) </li> +<li> K.3.5.3.3 The printf_s function (p: 593-594) </li> +<li> K.3.5.3.5 The snprintf_s function (p: 594-595) </li> +<li> K.3.5.3.6 The sprintf_s function (p: 595-596) </li> +</ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul> +<li> 7.19.6.1 The fprintf function (p: 274-282) </li> +<li> 7.19.6.3 The printf function (p: 290) </li> +<li> 7.19.6.5 The snprintf function (p: 290-291) </li> +<li> 7.19.6.6 The sprintf function (p: 291) </li> +</ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul> +<li> 4.9.6.1 The fprintf function </li> +<li> 4.9.6.3 The printf function </li> +<li> 4.9.6.5 The sprintf function </li> +</ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fwprintf" title="c/io/fwprintf"> <span class="t-lines"><span>wprintf</span><span>fwprintf</span><span>swprintf</span><span>wprintf_s</span><span>fwprintf_s</span><span>swprintf_s</span><span>snwprintf_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> prints formatted wide character output to <code><a href="std_streams" title="c/io/std streams">stdout</a></code>, a file stream or a buffer <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="vfprintf" title="c/io/vfprintf"> <span class="t-lines"><span>vprintf</span><span>vfprintf</span><span>vsprintf</span><span>vsnprintf</span><span>vprintf_s</span><span>vfprintf_s</span><span>vsprintf_s</span><span>vsnprintf_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> prints formatted output to <code><a href="std_streams" title="c/io/std streams">stdout</a></code>, a file stream or a buffer<br> using variable argument list <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fputs" title="c/io/fputs"> <span class="t-lines"><span>fputs</span></span></a></div> </td> <td> writes a character string to a file stream <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td> <div><a href="fscanf" title="c/io/fscanf"> <span class="t-lines"><span>scanf</span><span>fscanf</span><span>sscanf</span><span>scanf_s</span><span>fscanf_s</span><span>sscanf_s</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> reads formatted input from <code><a href="std_streams" title="c/io/std streams">stdin</a></code>, a file stream or a buffer <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/io/c/fprintf" title="cpp/io/c/fprintf">C++ documentation</a></span> for <code>printf, fprintf, sprintf, snprintf</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/io/fprintf" class="_attribution-link">https://en.cppreference.com/w/c/io/fprintf</a> + </p> +</div> |
