mbrlen(3)
๐ mbrlen โ the girl who counts bytes for you ๐
okay bestie, so this page is all about mbrlen, a lil function from the Linux man-pages, and she is literally the byte-counter of the multibyte world. she tells you how many bytes the next complete multibyte character is eating. no cap. ๐ฆ
๐ LIBRARY ๐
Standard C library (libc, -lc) โ she lives in there, rent free ๐
โจ SYNOPSIS โจ
#include <wchar.h>
size_t mbrlen(size_t n;
const char s[restrict n], size_t n,
mbstate_t *restrict ps);
yes bestie, that's the exact signature, don't come for me, I copied it character for character ๐ญ
๐ DESCRIPTION ๐
so here's the tea. mbrlen() peeks at at most n bytes of the multibyte string starting at s, and it extracts the next complete multibyte character. she also updates the shift state *ps while she's at it. main character energy fr.
- if that multibyte character is not the null wide character โ she returns the number of bytes that were consumed from
s๐ซ - if that multibyte character is the null wide character โ she resets the shift state
*psto the initial state and returns0๐
now, plot twist. if the n bytes starting at s don't contain a complete multibyte character, mbrlen() returns (size_t) -2. and yes, this can happen even if n >= MB_CUR_MAX, specifically if the multibyte string contains redundant shift sequences. the way that can just... happen ๐
and if the multibyte string starting at s contains an invalid multibyte sequence before the next complete character? she returns (size_t) -1 and sets errno to EILSEQ. in that case, the effects on *ps are undefined โ so like, don't touch that, she's not answering questions ๐
one more thing: if ps is NULL, a static anonymous state known only to the mbrlen() function is used instead. she's got a secret state, iykyk ๐คซ
๐ซถ RETURN VALUE ๐ซถ
here's the recap so you never have to ask twice:
- she returns the number of bytes parsed from the multibyte sequence starting at
s, if a non-null wide character was recognized โ - she returns
0, if a null wide character was recognized ๐ธ - she returns
(size_t) -1and setserrnotoEILSEQ, if an invalid multibyte sequence was encountered ๐ญ - she returns
(size_t) -2if she couldn't parse a complete multibyte character โ which literally just meansnshould be increased, bestie, go make it bigger ๐
โญ ATTRIBUTES โญ
want the whole explanation of the terms used up in this section? see attributes(7). iconic.
mbrlen()โ Thread safety โMT-Unsafe race:mbrlen/!ps๐
fr fr pay attention to that one. MT-Unsafe race:mbrlen/!ps is not a vibe you want in your threaded code.
๐ STANDARDS ๐
C11, POSIX.1-2008. certified, periodt.
๐ฆ HISTORY ๐ฆ
POSIX.1-2001, C99. she's been around, she's a legacy icon.
๐ NOTES ๐
the behavior of mbrlen() depends on the LC_CTYPE category of the current locale. so like, your locale is running the show, not her ๐
๐ SEE ALSO ๐
mbrtowc(3)โ her sibling, go say hi ๐ซถ
okay that's the whole page bestie, go forth and count your bytes ๐โจ
this was mbrlen(3) from Linux man-pages 6.20, dated 2026-02-08 โ mwah, slay, bye ๐๐
๐ฆ 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