summaryrefslogtreecommitdiff
path: root/devdocs/c/io%2Ffwprintf.html
blob: 5ed5fda8e22732ca9d2a9d25db6ad2c0ca50abe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    <h1 id="firstHeading" class="firstHeading">wprintf, fwprintf, swprintf, wprintf_s, fwprintf_s, swprintf_s, snwprintf_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;wchar.h&gt;</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-since-c95 t-until-c99"> <td> <pre data-language="c">int wprintf( const wchar_t *format, ... );</pre>
</td>  <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><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 wprintf( const wchar_t *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-since-c95 t-until-c99"> <td> <pre data-language="c">int fwprintf( FILE *stream, const wchar_t *format, ... );</pre>
</td>  <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><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 fwprintf( FILE *restrict stream,
              const wchar_t *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-since-c95 t-until-c99"> <td> <pre data-language="c">int swprintf( wchar_t *buffer, size_t bufsz,
              const wchar_t *format, ... );</pre>
</td>  <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><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 swprintf( wchar_t *restrict buffer, size_t bufsz,
              const wchar_t *restrict format, ... );</pre>
</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 wprintf_s( const wchar_t *restrict format, ... );</pre>
</td> <td> (4) </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 fwprintf_s( FILE *restrict stream,
                const wchar_t *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 swprintf_s( wchar_t *restrict buffer, rsize_t bufsz,
                const wchar_t *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 snwprintf_s( wchar_t *restrict s, rsize_t n,
                 const wchar_t *restrict format, ... );</pre>
</td> <td> (7) </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 wide string equivalents and writes the results to a variety of sinks.</p>
<div class="t-li1">
<span class="t-li">1)</span> Writes the results to <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 a file stream <code>stream</code>.</div> <div class="t-li1">
<span class="t-li">3)</span> If <code>bufsz</code> is greater than zero, writes the results to a wide string <code>buffer</code>. At most <code>bufsz-1</code> wide characters are written followed by null wide character. If <code>bufsz</code> is zero, nothing is written (and <code>buffer</code> may be a null pointer).</div> <div class="t-li1">
<span class="t-li">4-6)</span> Same as <span class="t-v">(1-3)</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: <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>format</code> or <code>buffer</code> is a null pointer </li>
<li> <code>bufsz</code> is zero or greater than <code>RSIZE_MAX/sizeof(wchar_t)</code> </li>
<li> encoding errors occur in any of string and character conversion specifiers </li>
<li> (only for <code>swprintf_s</code>) the number of wide characters to be written, including the null, would exceed <code>bufsz</code>.</li>
</ul>
</div> <div class="t-li1">
<span class="t-li">7)</span> Same as <span class="t-v">(6)</span>, except it will truncate the result to fit within the array pointed to by s. As with all bounds-checked functions, <code>wprintf_s</code> , <code>fwprintf_s</code>, <code>swprintf_s</code>, and <code>snwprintf_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>&lt;stdio.h&gt;</code></a>.</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 wide character string to write to </td>
</tr> <tr class="t-par"> <td> bufsz </td> <td> - </td> <td> up to <code>bufsz-1</code> wide 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 wide 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 wide 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>wchar_t</code> as if by calling <code><a href="../string/multibyte/btowc" title="c/string/multibyte/btowc">btowc</a></code>. If the <b>l</b> modifier is used, the <code>wint_t</code> argument is first converted to <code>wchar_t</code>.</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 a character array containing a multibyte character sequence beginning in the initial shift state, which is converted to wide character array as if by a call to <code><a href="../string/multibyte/mbrtowc" title="c/string/multibyte/mbrtowc">mbrtowc</a></code> with zero-initialized conversion state. <i>Precision</i> specifies the maximum number of wide characters to be written. If <i>Precision</i> is not specified, writes every wide characters 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>.</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 &gt; 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>&lt;inttypes.h&gt;</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 wide characters written if successful or negative value if an error occurred.</div> <div class="t-li1">
<span class="t-li">3)</span> Number of wide characters written (not counting the terminating null wide character) if successful or negative value if an encoding error occurred or if the number of characters to be generated was equal or greater than <code>size</code> (including when <code>size</code> is zero).</div> <div class="t-li1">
<span class="t-li">4,5)</span> Number of wide characters written if successful or negative value if an error occurred.</div> <div class="t-li1">
<span class="t-li">6)</span> Number of wide characters (not counting the terminating null) that were written to <code>buffer</code>. Returns a negative value on encoding errors and on overflow. Returns zero on all other errors.</div> <div class="t-li1">
<span class="t-li">7)</span> Number of wide characters (not counting the terminating null) that would have been written to <code>buffer</code> had <code>bufsz</code> been sufficiently large, or a negative value if an error occurs. (meaning, write was successful and complete only if the return is nonnegative and less than <code>bufsz</code>)</div> <h3 id="Notes"> Notes</h3> <p>While narrow strings provide <code><a href="fprintf" title="c/io/fprintf">snprintf</a></code>, which makes it possible to determine the required output buffer size, there is no equivalent for wide strings<span class="t-rev-inl t-since-c11"><span> (until <code>snwprintf_s</code>)</span><span><span class="t-mark-rev t-since-c11">(since C11)</span></span></span>, and in order to determine the buffer size, the program may need to call <code>swprintf</code>, check the result value, and reallocate a larger buffer, trying again until successful.</p>
<p><code>snwprintf_s</code>, unlike <code>swprintf_s</code>, will truncate the result to fit within the array pointed to by <code>buffer</code>, even though truncation is treated as an error by most bounds-checked functions.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;locale.h&gt;
#include &lt;wchar.h&gt;
 
int main(void)
{
    char narrow_str[] = "z\u00df\u6c34\U0001f34c";
                    // or "zß水🍌"
                    // or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9f\x8d\x8c";
    wchar_t warr[29]; // the expected string is 28 characters plus 1 null terminator
    setlocale(LC_ALL, "en_US.utf8");
    swprintf(warr, sizeof warr/sizeof *warr,
              L"Converted from UTF-8: '%s'", narrow_str);
    wprintf(L"%ls\n", warr);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">Converted from UTF-8: 'zß水🍌'</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.29.2.1 The fwprintf function (p: 403-410) </li>
<li> 7.29.2.3 The swprintf function (p: 416) </li>
<li> 7.29.2.11 The wprintf function (p: 421) </li>
<li> K.3.9.1.1 The fwprintf_s function (p: 628) </li>
<li> K.3.9.1.4 The swprintf_s function (p: 630-631) </li>
<li> K.3.9.1.13 The wprintf_s function (p: 637-638) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul>
<li> 7.24.2.1 The fwprintf function (p: 349-356) </li>
<li> 7.24.2.3 The swprintf function (p: 362) </li>
<li> 7.24.2.11 The wprintf function (p: 366) </li>
</ul>
</ul>             <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="fprintf" title="c/io/fprintf"> <span class="t-lines"><span>printf</span><span>fprintf</span><span>sprintf</span><span>snprintf</span><span>printf_s</span><span>fprintf_s</span><span>sprintf_s</span><span>snprintf_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> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="vfwprintf" title="c/io/vfwprintf"> <span class="t-lines"><span>vwprintf</span><span>vfwprintf</span><span>vswprintf</span><span>vwprintf_s</span><span>vfwprintf_s</span><span>vswprintf_s</span><span>vsnwprintf_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<br> or a buffer using variable argument list <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="fputws" title="c/io/fputws"> <span class="t-lines"><span>fputws</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> writes a wide string to a file stream <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/fwprintf" title="cpp/io/c/fwprintf">C++ documentation</a></span> for <code>wprintf, fwprintf, swprintf</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/io/fwprintf" class="_attribution-link">https://en.cppreference.com/w/c/io/fwprintf</a>
  </p>
</div>