mbsrtowcs(3)
๐ mbsrtowcs โ bestie edition ๐
okay so this is mbsrtowcs(3), a lil member of the Library Functions Manual family, and it's here to convert a multibyte string into a wide-character string โ but make it restartable. yes queen. we love a comeback story. โจ
๐ LIBRARY
Standard C library, aka libc, link it with -lc. she's built in, no extra shopping required ๐๏ธ
๐ SYNOPSIS
#include <wchar.h>
size_t mbsrtowcs(size_t dsize;
wchar_t dest[restrict dsize],
const char **restrict src,
size_t dsize, mbstate_t *restrict ps);
โจ DESCRIPTION
so here's the tea โ โ if dest is not NULL, we convert the multibyte string *src into a wide-character string, and it starts landing at dest. at most dsize wide characters get written to dest, no more, that's the cap bestie. and the shift state *ps gets updated along the way.
the conversion is basically done by calling mbrtowc(dest, *src, n, ps) over and over, where n is some positive number, for as long as that call succeeds โ then you bump dest up by one and bump *src up by however many bytes got eaten. ๐ฝ๏ธ
the conversion can stop for three reasons, and you need to know all three, this is not a drill:
- an invalid multibyte sequence showed up. in that case
*srcis left pointing right at the invalid sequence,(size_t) -1is returned, anderrnois set toEILSEQ. ๐ not the vibe. dsizenon-L'\0'wide characters have been stored atdest. in this case*srcis left pointing at the next multibyte sequence to be converted, and the number of wide characters written todestgets returned.- the multibyte string has been completely converted, including the terminating null wide character (
'\0') โ which has the side effect of bringing*psback to the initial state ๐ง (she's healed). in this case*srcis set toNULL, and the number of wide characters written todestgets returned, not counting the terminating null wide character.
if dest is NULL, then dsize is ignored entirely (yes, ignored, she said what she said), and the conversion proceeds just like above โ except the converted wide characters don't get written out to memory at all, and there's no length limit. free spirit behavior honestly. ๐ฆ
in both of the cases above, if ps is NULL, then a static anonymous state that only mbsrtowcs() knows about is used instead. secret state. mysterious. iconique. ๐
now listen, to dodge that case 2 situation, the programmer should make sure dsize is greater than or equal to mbsrtowcs(NULL,src,0,ps)+1. simple math bestie, do it.
โ ๏ธ also the programmer must make sure there's room for at leastdsizewide characters atdest. no cap. if you don't, that's on you.
and for the lore: this function is a restartable version of mbstowcs(3). glow up achieved. ๐
๐ฅ RETURN VALUE
it returns the number of wide characters that make up the converted part of the wide-character string โ not including the terminating null wide character. and if an invalid multibyte sequence got encountered? (size_t) -1 comes back and errno is set to EILSEQ. periodt.
๐ ATTRIBUTES
for an explanation of the terms used in this section, go see attributes(7). here's the rundown:
mbsrtowcs()โ Thread safety โMT-Unsafe race:mbsrtowcs/!ps
๐ธ STANDARDS
C11, POSIX.1-2008.
๐ HISTORY
POSIX.1-2001, C99.
๐ NOTES
the behavior of mbsrtowcs() depends on the LC_CTYPE category of the current locale. locale girlies know.
๐ passingNULLaspsis not multithread safe. so likeโฆ don't. unless you're chill with chaos.
๐ SEE ALSO
iconv(3), mbrtowc(3), mbsinit(3), mbsnrtowcs(3), mbstowcs(3)
okay that's the whole page bestie, you're officially fluent ๐ซถ this was Linux man-pages 6.20, dated 2026-02-08 โ mwah, kiss the manual, go convert some strings responsibly. ๐โจ๐
๐ฆ 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