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 most MB_CUR_MAX bytes will be written at s. so you, the programmer, must make sure there's enough room to store that multibyte sequence at s. 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 the LC_CTYPE category of the current locale. so like... your locale is the moment, and the moment matters. ๐ŸŒธ
  • passing NULL as ps is 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