liveBook · Manning

Multibyte string

As explained in Modern C, Third Edition

A multibyte string is a character sequence in which one logical character may occupy multiple bytes according to the active C locale encoding.

Printing extended characters

Call setlocale(LC_ALL, "") near the start of a program, then output a string containing extended characters. Without the locale switch, those characters may appear as garbage on the terminal. section 14.3

 setlocale(LC_ALL, "");
   /* Multibyte character printing only works after the locale
      has been switched. */
   draw_sep(TOPLEFT " © 2014 j𝜀nz ' g𝜔z,t𝜀t ", TOPRIGHT);

The listing places setlocale before output containing extended characters. section 14.3

A multibyte character is a sequence of bytes interpreted as one extended character. A multibyte string contains such characters, but remains compatible with ordinary C strings: multibyte characters contain no null bytes, and the string ends with a null byte. Thus byte-oriented functions such as strcpy can still copy it. section 14.3

TAKEAWAY: byte-oriented string operations do not necessarily answer character-oriented questions.

  • strlen reports bytes, not the number of printed characters. The example uses a separate, nonstandard mbsrlen function when formatting an output line. section 14.3
  • Conversions can require an mbstate_t state. A restartable conversion retains progress when an input buffer ends before a multibyte character is complete. section 14.5
 size_t mbsrlen(char const*s, mbstate_t const*restrict state) {
   state = state ? state : MBSTATE;
   mbstate_t st = *state;
   size_t mblen = mbsrtowcs(nullptr, &s, 0, &st);
   if (mblen == mbinvalid) errno = 0;
   return mblen;
 }

This mbsrlen implementation copies the conversion state and uses mbsrtowcs to obtain a converted length without allocating a wide-character string. section 14.3

Scanning buffered input

For input read through fgets, setlocale(LC_CTYPE, "") selects the platform’s multibyte encoding. A loop passes successive portions of the buffer to mbrtoc8 with an mbstate_t. An incomplete result means scanning must continue with the saved state; invalid input takes an error path. When conversion completes, the example examines the resulting UTF-8 bytes. section 14.5 section 14.7

How Modern C, Third Edition covers it

How deep it goes
Defines byte sequences and null termination, then works through character counting and restartable conversion; it does not specify a universal multibyte encoding.
Examples
Shows locale-aware terminal output, a conversion-based length function, and an fgets loop scanning buffered input into UTF-8.
What it assumes
Assumes familiarity with C strings, char arrays, and basic I/O while defining multibyte characters and introducing conversion state.
What you can do after
Apply it via C library locale and conversion functions to print extended text and scan incomplete multibyte input.
How it connects
Connects multibyte strings to C locales, null-terminated strings, wide characters, UTF-8, and conversion state.

Also explained in

Compare how 2 books explain multibyte string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.