1MBRTOWC(3) Linux Programmer's Manual MBRTOWC(3)
2
3
4
6 mbrtowc - convert a multibyte sequence to a wide character
7
9 #include <wchar.h>
10
11 size_t mbrtowc(wchar_t *restrict pwc, const char *restrict s, size_t n,
12 mbstate_t *restrict ps);
13
15 The main case for this function is when s is not NULL and pwc is not
16 NULL. In this case, the mbrtowc() function inspects at most n bytes of
17 the multibyte string starting at s, extracts the next complete multi‐
18 byte character, converts it to a wide character and stores it at *pwc.
19 It updates the shift state *ps. If the converted wide character is not
20 L'\0' (the null wide character), it returns the number of bytes that
21 were consumed from s. If the converted wide character is L'\0', it re‐
22 sets the shift state *ps to the initial state and returns 0.
23
24 If the n bytes starting at s do not contain a complete multibyte char‐
25 acter, mbrtowc() returns (size_t) -2. This can happen even if n >=
26 MB_CUR_MAX, if the multibyte string contains redundant shift sequences.
27
28 If the multibyte string starting at s contains an invalid multibyte se‐
29 quence before the next complete character, mbrtowc() returns
30 (size_t) -1 and sets errno to EILSEQ. In this case, the effects on *ps
31 are undefined.
32
33 A different case is when s is not NULL but pwc is NULL. In this case,
34 the mbrtowc() function behaves as above, except that it does not store
35 the converted wide character in memory.
36
37 A third case is when s is NULL. In this case, pwc and n are ignored.
38 If the conversion state represented by *ps denotes an incomplete multi‐
39 byte character conversion, the mbrtowc() function returns (size_t) -1,
40 sets errno to EILSEQ, and leaves *ps in an undefined state. Otherwise,
41 the mbrtowc() function puts *ps in the initial state and returns 0.
42
43 In all of the above cases, if ps is NULL, a static anonymous state
44 known only to the mbrtowc() function is used instead. Otherwise, *ps
45 must be a valid mbstate_t object. An mbstate_t object a can be ini‐
46 tialized to the initial state by zeroing it, for example using
47
48 memset(&a, 0, sizeof(a));
49
51 The mbrtowc() function returns the number of bytes parsed from the
52 multibyte sequence starting at s, if a non-L'\0' wide character was
53 recognized. It returns 0, if a L'\0' wide character was recognized.
54 It returns (size_t) -1 and sets errno to EILSEQ, if an invalid multi‐
55 byte sequence was encountered. It returns (size_t) -2 if it couldn't
56 parse a complete multibyte character, meaning that n should be in‐
57 creased.
58
60 For an explanation of the terms used in this section, see at‐
61 tributes(7).
62
63 ┌─────────────────────────┬───────────────┬────────────────────────────┐
64 │Interface │ Attribute │ Value │
65 ├─────────────────────────┼───────────────┼────────────────────────────┤
66 │mbrtowc() │ Thread safety │ MT-Unsafe race:mbrtowc/!ps │
67 └─────────────────────────┴───────────────┴────────────────────────────┘
68
70 POSIX.1-2001, POSIX.1-2008, C99.
71
73 The behavior of mbrtowc() depends on the LC_CTYPE category of the cur‐
74 rent locale.
75
77 mbsinit(3), mbsrtowcs(3)
78
80 This page is part of release 5.12 of the Linux man-pages project. A
81 description of the project, information about reporting bugs, and the
82 latest version of this page, can be found at
83 https://www.kernel.org/doc/man-pages/.
84
85
86
87GNU 2021-03-22 MBRTOWC(3)