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

