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 mostnbytes of the multibyte string that starts ats, 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
nbytes starting atsdon't contain a complete multibyte character,mblen()returns -1. ๐ญ and listen โ this can happen even ifnis greater than or equal toMB_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
scontains 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