mbtowc(3)
π mbtowc(3) β bestie edition π
okay so we're doing mbtowc(3) today, from the Library Functions Manual, and honestly? she's a classic. a little old-school, a little haunted, but we love her. she takes a multibyte sequence and turns it into a wide character. no cap.
β¨ NAME β¨
mbtowc β convert a multibyte sequence to a wide character π¦
π LIBRARY π
Standard C library (libc, -lc). yes queen, it's already linked, you're fine.
πΈ SYNOPSIS πΈ
#include <stdlib.h>
int mbtowc(size_t size;
wchar_t *restrict pwc, const char s[restrict size],
size_t size);
(no i will not explain that signature. she is what she is. π )
π DESCRIPTION π
okay so there are three whole cases and you literally have to know all three, bestie, so let's go.
Case one: s is not NULL AND pwc is not NULL. This is the main character case, fr fr. mbtowc() peeks at at most size bytes of the multibyte string starting at s, pulls out the next complete multibyte character, converts it to a wide character, and stores it at *pwc. she also updates an internal shift state that only mbtowc() knows about (iykyk). And here's the return rule: if s does not point to a null byte ('\0'), you get back the number of bytes consumed from s β but if it does point to a null byte, you get 0.
Case two: s is not NULL but pwc is NULL. she behaves exactly like above, except she does not actually store the converted wide character in memory. she just... does the vibe check and moves on. π
Case three: s is NULL. now pwc and size get completely ignored. mbtowc() resets the shift state (the one only she knows about) back to the initial state, and returns nonzero if the encoding has nontrivial shift state, or zero if the encoding is stateless.
π¨ and now the part that ruins your day: if thesizebytes starting atsdon't contain a complete multibyte character, or if they contain an invalid multibyte sequence,mbtowc()returns-1. and get this β this can happen even ifsize >= MB_CUR_MAX, if the multibyte string contains redundant shift sequences. yes, even when the math says you're fine. π
π RETURN VALUE π
- If
sis notNULL: you get the number of consumed bytes starting ats, or0ifspoints to a null byte, or-1upon failure. - If
sisNULL: you get nonzero if the encoding has nontrivial shift state, or zero if the encoding is stateless.
that's it. that's the whole chart. π
π ATTRIBUTES π
for an explanation of the terms used in this section, go see attributes(7). (their words not mine, but valid.)
- Interface:
mbtowc() - Attribute: Thread safety
- Value: MT-Unsafe race
she races. she is not the friend you text in a multithreaded emergency. π
π VERSIONS π
this function is not multithread safe. periodt. and if that's a problem for you, mbrtowc(3) provides a better interface to the same functionality β so like, go there, she's evolved.
β STANDARDS β
C11, POSIX.1-2008. certified. π
π¦ HISTORY π¦
POSIX.1-2001, C99. she's been here a minute.
π NOTES π
the behavior of mbtowc() depends on the LC_CTYPE category of the current locale. change the locale, change the girl. π
π₯ SEE ALSO π₯
okay that's the whole page, bestie, you ate it, you understood it, iconic behavior. this was Linux man-pages 6.20, dated 2026-02-08, and it's page mbtowc(3). love u, go read the mbrtowc(3) one too, she's the upgrade. π«Άπβ¨
π¦ 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