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 *src is left pointing right at the invalid sequence, (size_t) -1 is returned, and errno is set to EILSEQ. ๐Ÿ’€ not the vibe.
  • dsize non-L'\0' wide characters have been stored at dest. in this case *src is left pointing at the next multibyte sequence to be converted, and the number of wide characters written to dest gets returned.
  • the multibyte string has been completely converted, including the terminating null wide character ('\0') โ€” which has the side effect of bringing *ps back to the initial state ๐Ÿงš (she's healed). in this case *src is set to NULL, and the number of wide characters written to dest gets 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 least dsize wide characters at dest. 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.

๐Ÿ’€ passing NULL as ps is 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