MULTIBYTE(3)
πΈ multibyte(3) β bestie edition πΈ
okay so bestie, welcome to the multibyte(3) page from the Library Functions Manual β this is the one that explains how the C library handles multibyte and wide character manipulation functions. it's giving "let me explain CJK encodings to you in the group chat" energy and honestly? we love her for it π
π LIBRARY
Standard C Library β link it with -lc. that's it, that's the whole vibe π«Ά
β¨ SYNOPSIS β¨
#include <limits.h>
#include <stdlib.h>
#include <wchar.h>
three headers, no cap. collect them all π
π DESCRIPTION π
okay so here's the situation. the basic elements of some written natural languages β think Chinese, for example β literally cannot be represented uniquely with single C chars. like, it's just not happening. so the C standard supports two different ways of dealing with extended natural language encodings:
- wide characters β an internal representation where each basic element maps to a single object of type
wchar_t - multibyte characters β used for input and output, where each basic element is coded as a sequence of C
chars
and here's the tea: individual basic elements may map into one or more (up to MB_LEN_MAX) bytes in a multibyte character. so like, sometimes one glyph is one byte, sometimes it's several. it depends. main character energy honestly π
The current locale (setlocale(3)) governs the interpretation of wide and multibyte characters. The locale category LC_CTYPE specifically controls this interpretation. The wchar_t type is wide enough to hold the largest value in the wide character representations for all locales. iconic of it β¨
Multibyte strings may contain βshiftβ indicators to switch to and from particular modes within the given representation. If explicit bytes are used to signal shifting, these are not recognized as separate characters but are lumped with a neighboring character. There is always a distinguished βinitialβ shift state. Some functions (e.g., mblen(3), mbtowc(3) and wctomb(3)) maintain static shift state internally, whereas others store it in an mbstate_t object passed by the caller.
β οΈ bestie listen. Shift states are undefined after a call tosetlocale(3)with theLC_CTYPEorLC_ALLcategories. like, undefined. do not come for me when it breaks. i warned you π
For convenience in processing, the wide character with value 0 (the null wide character) is recognized as the wide character string terminator, and the character with value 0 (the null byte) is recognized as the multibyte character string terminator. And null bytes are not permitted within multibyte characters β that's a hard no π
π§ the function lineup π§
The C library provides the following functions for dealing with multibyte characters β presenting the whole cast, no cuts:
mblen(3)β get number of bytes in a charactermbrlen(3)β get number of bytes in a character (restartable)mbrtowc(3)β convert a character to a wide-character code (restartable)mbsrtowcs(3)β convert a character string to a wide-character string (restartable)mbstowcs(3)β convert a character string to a wide-character stringmbtowc(3)β convert a character to a wide-character codewcrtomb(3)β convert a wide-character code to a character (restartable)wcstombs(3)β convert a wide-character string to a character stringwcsrtombs(3)β convert a wide-character string to a character string (restartable)wctomb(3)β convert a wide-character code to a character
notice how the restartable ones are all labeled? that's the mbstate_t girlies vs the static-state girlies. iykyk π
π SEE ALSO π
go read these next, bestie, in this exact order:
localedef(1), setlocale(3), stdio(3), big5(5), euc(5), gb18030(5), gb2312(5), gbk(5), mskanji(5), utf8(5)
that's the whole reading list. yes, all five of the encoding pages. slay π¦
β STANDARDS β
These functions conform to ISO/IEC 9899:1999 (βISO C99β). certified. periodt π
and that's the page!! Debian, September 9, 2019 β that's who shipped this and when, mwah, thanks Debian ily π«Άπβ¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved
- Copyright (c) 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms