mblen(3)

๐Ÿ’– mblen โ€” bestie's guide to counting bytes in your next multibyte character ๐Ÿ’–

okay so real talk, this is mblen(3), and it lives in the Standard C library (that's libc, aka -lc, aka the thing you already linked). it's giving "tiny function, big feelings" because it counts how many bytes your next multibyte character is eating. ๐Ÿฆ‹


โœจ SYNOPSIS โœจ

#include <stdlib.h>

int mblen(size_t n;
          const char s[n], size_t n);

bestie. i need you to look at that signature. i'm not fixing it. that's literally what it says. ๐Ÿ’…


๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay so here's the tea, and it depends on whether s is NULL or not, because it's giving two completely different main character eras. ๐Ÿ‘‘

if s is NOT NULL:

  • mblen() peeks at at most n bytes of the multibyte string that starts at s, and it pulls out the next complete multibyte character. no cap.
  • it uses a static anonymous shift state that only mblen() itself knows about. it's lowkey mysterious like that. ๐Ÿ™ˆ
  • if that multibyte character is not the null wide character, it returns the number of bytes it consumed from s. slay.
  • if that multibyte character is the null wide character, it returns 0.

if things go wrong:

  • if the n bytes starting at s don't contain a complete multibyte character, mblen() returns -1. ๐Ÿ˜ญ and listen โ€” this can happen even if n is greater than or equal to MB_CUR_MAX, if the multibyte string contains redundant shift sequences. so don't come for it, it's not being dramatic for no reason.
  • if the multibyte string starting at s contains an invalid multibyte sequence before the next complete character, mblen() also returns -1. ๐Ÿ’€

if s IS NULL:

  • mblen() resets that shift state (the one only this function knows about, yes, that one) back to the initial state.
  • and then it returns nonzero if the encoding has nontrivial shift state, or zero if the encoding is stateless. it's a vibe check for your encoding, honestly. โœจ

๐Ÿ’… RETURN VALUE ๐Ÿ’…

  • number of bytes parsed from the multibyte sequence starting at s, if a non-null wide character was recognized. go off. ๐ŸŽ€
  • 0, if a null wide character was recognized.
  • -1, if an invalid multibyte sequence was encountered, or if it couldn't parse a complete multibyte character.
fr fr, that -1 is doing a lot of emotional labor. it covers two whole different sad situations. ๐Ÿซถ

๐Ÿ”ฅ ATTRIBUTES ๐Ÿ”ฅ

for the explanation of the terms used in this section, mosey on over to attributes(7). here's the chart, but make it cute:

  • Interface: mblen()
    • Attribute: Thread safety
    • Value: MT-Unsafe race
not to be that girl but MT-Unsafe race?? yeah. be careful out here bestie. ๐ŸŒˆ

๐ŸŒŸ VERSIONS ๐ŸŒŸ

the function mbrlen(3) provides a better interface to the same functionality. so like, mbrlen(3) is the glow-up version. periodt. ๐Ÿ’œ


โญ STANDARDS โญ

C11, POSIX.1-2008. certified. iconic. no notes. ๐Ÿ‘‘


๐ŸŒธ HISTORY ๐ŸŒธ

POSIX.1-2001, C99. it's been around, it's seen things. ๐Ÿงš


๐Ÿฆ‹ NOTES ๐Ÿฆ‹

the behavior of mblen() depends on the LC_CTYPE category of the current locale. so if it's acting weird, check your locale first, okay?? don't fight the function. ๐Ÿ˜ญ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ

  • mbrlen(3) โ€” go say hi to the better interface

and that's the whole page!! ๐Ÿ’– this absolutely gorgeous piece of documentation came from Linux man-pages 6.20, dated 2026-02-08, and yes she ate, she left no crumbs. ๐ŸŽ€โœจ๐Ÿ’…

๐Ÿ“ฆ 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