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 *ps to the initial state and returns 0 ๐ŸŽ€

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) -1 and sets errno to EILSEQ, if an invalid multibyte sequence was encountered ๐Ÿ˜ญ
  • she returns (size_t) -2 if she couldn't parse a complete multibyte character โ€” which literally just means n should 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