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
|
<h1 id="firstHeading" class="firstHeading">wctomb, wctomb_s</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">int wctomb( char *s, wchar_t wc );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">errno_t wctomb_s( int *restrict status, char *restrict s, rsize_t ssz, wchar_t wc );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <div class="t-li1">
<span class="t-li">1)</span> Converts a wide character <code>wc</code> to multibyte encoding and stores it (including any shift sequences) in the char array whose first element is pointed to by <code>s</code>. No more than <code>MB_CUR_MAX</code> characters are stored. The conversion is affected by the current locale's LC_CTYPE category. </div> <div class="t-li1">
If <code>wc</code> is the null character, the null byte is written to <code>s</code>, preceded by any shift sequences necessary to restore the initial shift state.</div> <div class="t-li1">
If <code>s</code> is a null pointer, this function resets the global conversion state and determines whether shift sequences are used.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the result is returned in the out-parameter <code>status</code> and 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> <code>ssz</code> is less than the number of bytes that would be written (unless <code>s</code> is null) </li>
<li> <code>ssz</code> is greater than <code>RSIZE_MAX</code> (unless <code>s</code> is null) </li>
<li> <code>s</code> is a null pointer but <code>ssz</code> is not zero </li>
</ul> </dd>
<dd>As with all bounds-checked functions, <code>wctomb_s</code> 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="../../program" title="c/program"><code><stdlib.h></code></a>.</dd>
</dl>
</div> <h3 id="Notes"> Notes</h3> <p>Each call to <code>wctomb</code> updates the internal global conversion state (a static object of type <code><a href="mbstate_t" title="c/string/multibyte/mbstate t">mbstate_t</a></code>, known only to this function). If the multibyte encoding uses shift states, this function is not reentrant. In any case, multiple threads should not call <code>wctomb</code> without synchronization: <code><a href="wcrtomb" title="c/string/multibyte/wcrtomb">wcrtomb</a></code> or <code>wctomb_s</code> may be used instead.</p>
<p>Unlike most bounds-checked functions, <code>wctomb_s</code> does not null-terminate its output, because it is designed to be used in loops that process strings character-by-character.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> s </td> <td> - </td> <td> pointer to the character array for output </td>
</tr> <tr class="t-par"> <td> wc </td> <td> - </td> <td> wide character to convert </td>
</tr> <tr class="t-par"> <td> ssz </td> <td> - </td> <td> maximum number of bytes to write to <code>s</code> (size of the array <code>s</code>) </td>
</tr> <tr class="t-par"> <td> status </td> <td> - </td> <td> pointer to an out-parameter where the result (length of the multibyte sequence or the shift sequence status) will be stored </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> If <code>s</code> is not a null pointer, returns the number of bytes that are contained in the multibyte representation of <code>wc</code> or <code>-1</code> if <code>wc</code> is not a valid character.</div> <div class="t-li1">
If <code>s</code> is a null pointer, resets its internal conversion state to represent the initial shift state and returns <code>0</code> if the current multibyte encoding is not state-dependent (does not use shift sequences) or a non-zero value if the current multibyte encoding is state-dependent (uses shift sequences).</div> <div class="t-li1">
<span class="t-li">2)</span> zero on success, in which case the multibyte representation of <code>wc</code> is stored in <code>s</code> and its length is stored in <code>*status</code>, or, if <code>s</code> is null, the shift sequence status is stored in <code>status</code>). Non-zero on encoding error or runtime constraint violation, in which case <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">1</span></code> is stored in <code>*status</code>. The value stored in <code>*status</code> never exceeds <code>MB_CUR_MAX</code>
</div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
void demo(wchar_t wc)
{
const char* dep = wctomb(NULL, wc) ? "Yes" : "No";
printf("State-dependent encoding? %s.\n", dep);
char mb[MB_CUR_MAX];
int len = wctomb(mb, wc);
printf("wide char '%lc' -> multibyte char [", wc);
for (int idx = 0; idx < len; ++idx)
printf("%s%#2x", idx ? " " : "", (unsigned char)mb[idx]);
printf("]\n");
}
int main(void)
{
setlocale(LC_ALL, "en_US.utf8");
printf("MB_CUR_MAX = %zu\n", MB_CUR_MAX);
demo(L'A');
demo(L'\u00df');
demo(L'\U0001d10b');
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">MB_CUR_MAX = 6
State-dependent encoding? No.
wide char 'A' -> multibyte char [0x41]
State-dependent encoding? No.
wide char 'ß' -> multibyte char [0xc3 0x9f]
State-dependent encoding? No.
wide char '𝄋' -> multibyte char [0xf0 0x9d 0x84 0x8b]</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul>
<li> 7.22.7.3 The wctomb function (p: 261) </li>
<li> K.3.6.4.1 The wctomb_s function (p: 443) </li>
</ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.22.7.3 The wctomb function (p: 358-359) </li>
<li> K.3.6.4.1 The wctomb_s function (p: 610-611) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.7.3 The wctomb function (p: 322-323) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.7.3 The wctomb function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="mbtowc" title="c/string/multibyte/mbtowc"> <span class="t-lines"><span>mbtowc</span></span></a></div> </td> <td> converts the next multibyte character to wide character <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="wcrtomb" title="c/string/multibyte/wcrtomb"> <span class="t-lines"><span>wcrtomb</span><span>wcrtomb_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-c11">(C11)</span></span></span></div> </td> <td> converts a wide character to its multibyte representation, given state <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/string/multibyte/wctomb" title="cpp/string/multibyte/wctomb">C++ documentation</a></span> for <code>wctomb</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/string/multibyte/wctomb" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/wctomb</a>
</p>
</div>
|