mbrtowc(3)
๐ mbrtowc(3) โ convert a multibyte sequence to a wide character ๐
okay bestie, welcome to the Library Functions Manual, section 3, page mbrtowc(3). this one's a lil crunchy but i got you, fr fr ๐ซถ
๐ LIBRARY
Standard C library (libc, -lc) โ yeah she lives in libc, standard issue, no cap.
โจ SYNOPSIS โจ
#include <wchar.h>
size_t mbrtowc(size_t n;
wchar_t *restrict pwc, const char s[restrict n],
size_t n, mbstate_t *restrict ps);
like. look at that prototype. it's giving chaos but we move ๐
๐ง DESCRIPTION
okay so the main case is when s is not NULL and pwc is not NULL. in that case, mbrtowc() peeks at at most n bytes of the multibyte string starting at s, yoinks the next complete multibyte character, converts it to a wide character, and drops it at *pwc. it also updates the shift state *ps. then:
- if the converted wide character is not
L'\0'(the null wide character), she returns the number of bytes that were consumed froms. ๐ - if the converted wide character is
L'\0', she resets the shift state*psto the initial state and returns0.
now. if the n bytes starting at s do not contain a complete multibyte character, mbrtowc() returns (size_t) -2. and bestie, listen โ this can happen even if n >= MB_CUR_MAX, if the multibyte string contains redundant shift sequences. sneaky.
if the multibyte string starting at s contains an invalid multibyte sequence before the next complete character, mbrtowc() returns (size_t) -1 and sets errno to EILSEQ. and in this case? the effects on *ps are undefined. not a vibe. ๐
a different case is when s is not NULL but pwc is NULL. then mbrtowc() behaves exactly as above, except she does not store the converted wide character in memory. she's just doing the conversion and keeping it to herself, main character energy.
a third case is when s is NULL. in this case, pwc and n are ignored. so:
- if the conversion state represented by
*psdenotes an incomplete multibyte character conversion,mbrtowc()returns(size_t) -1, setserrnotoEILSEQ, and leaves*psin an undefined state. ๐ซ - otherwise,
mbrtowc()puts*psin the initial state and returns0. slay.
in all of the above cases, if ps is NULL, a static anonymous state known only to the mbrtowc() function is used instead. otherwise, *ps must be a valid mbstate_t object. and bestie, an mbstate_t object a can be initialized to the initial state by zeroing it, for example using:
memset(&a, 0, sizeof(a));
๐ RETURN VALUE
so what does she give back? depends:
- the number of bytes parsed from the multibyte sequence starting at
s, if a non-L'\0'wide character was recognized ๐ธ 0, if aL'\0'wide character was recognized(size_t) -1and setserrnotoEILSEQ, if an invalid multibyte sequence was encountered ๐ญ(size_t) -2if the consumednbytes form a valid partial character sequence โ meaning more subsequent data needs to be fed in. she's waiting, she's patient, she's hydrated ๐ง
๐ท๏ธ ATTRIBUTES
for an explanation of the terms used in this section, see attributes(7). (no tables on my watch, so here it is all cute:)
- Interface:
mbrtowc() - Attribute: Thread safety
- Value:
MT-Unsafe race:mbrtowc/!ps
ok so thread safety saidMT-Unsafe race:mbrtowc/!ps. read it. respect it. she is not out here being safe across threads unless you handlepsyourself, iykyk โจ
๐ STANDARDS
C11, POSIX.1-2008. certified, standardized, iconic.
๐ฐ๏ธ HISTORY
POSIX.1-2001, C99. she's been around. a legend from the jump ๐
๐ NOTES
the behavior of mbrtowc() depends on the LC_CTYPE category of the current locale. so yeah, set your locale or she'll be confused. ๐ฆ
๐ SEE ALSO
and that's the whole page bestie!! this was Linux man-pages 6.20, dated 2026-02-08 โ go forth and convert those multibyte sequences responsibly, mwah ๐ ๐๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-2.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms