wcrtomb(3)
๐ wcrtomb(3) โ bestie edition
okay so this is a libc page from the Library Functions Manual, and it's all about wcrtomb โ converting a wide character into a multibyte sequence โจ
๐ธ NAME ๐ธ
wcrtomb โ the girl who converts a wide character to a multibyte sequence ๐ฆ
๐ LIBRARY ๐
Standard C library โ link it with -lc ๐
โจ SYNOPSIS โจ
#include <wchar.h>
size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict ps);
๐ซถ DESCRIPTION ๐ซถ
okay so there are like, three whole situations here and we're gonna go through them one by one, no skipping, that's the rule ๐
Case one, the main character case: s is not NULL and wc is not a null wide character (L'\0'). in this case, wcrtomb() takes the wide character wc, converts it to its multibyte representation, and stores it right at the beginning of the character array pointed to by s. it updates the shift state *ps, and returns the length of that multibyte representation โ which is literally just the number of bytes written at s. slay. ๐ฅ
Case two, the plot twist: s is not NULL, but wc is a null wide character (L'\0'). here, wcrtomb() stores at the character array pointed to by s the shift sequence needed to bring *ps back to the initial state, followed by a '\0' byte. it updates the shift state *ps (aka brings it into the initial state, we love a reset arc ๐ง), and returns the length of the shift sequence plus one โ that is, the number of bytes written at s.
Case three, the chaotic one: s is NULL. in this case wc is just... ignored ๐, and the function effectively returns
wcrtomb(buf, L'\0', ps)
where buf is an internal anonymous buffer. it's giving mysterious, it's giving private, we don't get to see it. periodt. ๐
and in all of the above cases โ every single one bestie โ if ps is NULL, then a static anonymous state known only to the wcrtomb() function is used instead. iconic behaviour honestly ๐
โ ๏ธ at mostMB_CUR_MAXbytes will be written ats. so you, the programmer, must make sure there's enough room to store that multibyte sequence ats. no cap, do not skip this, she will not warn you twice. ๐ญ
๐ RETURN VALUE ๐
wcrtomb() returns the number of bytes that have been โ or would have been โ written to the byte array at s. ๐
but if wc cannot be represented as a multibyte sequence (as per the current locale), then (size_t) -1 gets returned, and errno is set to EILSEQ. rough, but that's the vibe check failing. ๐
๐ ATTRIBUTES ๐
for an explanation of the terms used in this section, go look at attributes(7) โ she's the reference for all this, iykyk ๐
- Interface:
wcrtomb() - Attribute: Thread safety
- Value:
MT-Unsafe race:wcrtomb/!ps
so basically: thread-safe? not entirely, bestie. the /!ps is doing a lot of heavy lifting there. ๐
โญ STANDARDS โญ
C11, POSIX.1-2008. โจ certified, standardised, official.
๐ HISTORY ๐
POSIX.1-2001, C99. she's been around a minute, and she ate. ๐
๐ NOTES ๐
- the behavior of
wcrtomb()depends on theLC_CTYPEcategory of the current locale. so like... your locale is the moment, and the moment matters. ๐ธ - passing
NULLaspsis not multithread safe. big deal, say it louder for the people in the back. ๐ข
๐ฆ SEE ALSO ๐ฆ
okay that's the whole page bestie, you're so welcome, go convert some wide characters responsibly ๐
this was Linux man-pages 6.20, dated 2026-02-08, and yes wcrtomb(3) said what she said. bye bestie, drink water, use MB_CUR_MAX, don't overflow your buffer ๐ซถโจ๐
๐ฆ 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