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 from s. ๐ŸŽ€
  • if the converted wide character is L'\0', she resets the shift state *ps to the initial state and returns 0.

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 *ps denotes an incomplete multibyte character conversion, mbrtowc() returns (size_t) -1, sets errno to EILSEQ, and leaves *ps in an undefined state. ๐Ÿซ 
  • otherwise, mbrtowc() puts *ps in the initial state and returns 0. 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 a L'\0' wide character was recognized
  • (size_t) -1 and sets errno to EILSEQ, if an invalid multibyte sequence was encountered ๐Ÿ˜ญ
  • (size_t) -2 if the consumed n bytes 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 said MT-Unsafe race:mbrtowc/!ps. read it. respect it. she is not out here being safe across threads unless you handle ps yourself, 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