liveBook · Manning

Wide character

As explained in Modern C

A wide character is a C character representation of type wchar_t intended to encode one character of an extended character set.

A wide character is a C character representation of type wchar_t, intended to represent one character of an extended character set. The header wchar.h provides wchar_t and utilities for wide and multibyte characters. section 14.3

Counting before conversion

The nonstandard function mbsrlen interprets a multibyte string as a wide character string and returns its length, or -1 on an encoding error. Unlike strlen, it does not simply count char array elements. Its parsing state must be consistent with the start of the multibyte character; mbsrlen does not modify that state. section 14.3

38   wchar_t* mbsrdup(char const*s, mbstate_t const*restrict state) {
39     size_t mblen = mbsrlen(s, state);
40     if (mblen == -1) return 0;
41     mbstate_t st = state ? *state : *MBSTATE;
42     wchar_t* S = malloc(sizeof(wchar_t[mblen+1]));
43     /* We know that s converts well, so no error check */ 
44     if (S) mbsrtowcs(S, &s, mblen+1, &st);
45     return S;
46   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":32}],[{\"line\":0,\"ch\":36},{\"line\":0,\"ch\":45}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":16}],[{\"line\":0,\"ch\":27},{\"line\":0,\"ch\":32}],[{\"line\":0,\"ch\":52},{\"line\":0,\"ch\":60}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":9}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":2,\"ch\":24},{\"line\":2,\"ch\":30}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":36},{\"line\":0,\"ch\":45}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":16}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":4,\"ch\":27},{\"line\":4,\"ch\":33}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":9}],[{\"line\":6,\"ch\":14},{\"line\":6,\"ch\":23}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":2,\"ch\":24},{\"line\":2,\"ch\":30}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}]]"}
!@%STYLE%@!

The mbsrdup example first obtains the wide-character length, allocates space for that many wchar_t elements plus a terminator, and converts the multibyte string with mbsrtowcs. section 14.3

TAKEAWAY: A multibyte string remains null terminated, but its number of printed characters cannot generally be inferred from strlen. Parsing its characters may require an mbstate_t conversion state. section 14.3

How Modern C covers it

How deep it goes
Works through length calculation and conversion with state, including helper implementations.
Examples
Shows mbsrlen counting wide characters and mbsrdup allocating and converting a multibyte string.
What it assumes
Assumes familiarity with ordinary C strings and introduces parsing state for multibyte strings.
What you can do after
Apply it using mbsrtowcs and mbstate_t to process multibyte strings as wide characters.
How it connects
Connects multibyte strings, conversion state, Unicode, and character encoding.

Also explained in

Compare how 2 books explain wide character →

Related concepts

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