liveBook · Manning

Mbrtowc

As explained in Modern C

mbrtowc is a C standard library function that converts one multibyte character to a wide character while tracking conversion state.

Suppose a multibyte string must be searched one character at a time. The number of bytes in its next character may not be known in advance. mbrtowc converts that character to a wide character, using len to limit how many bytes it examines. Its state argument lets successive conversions retain the context needed to interpret the string. section 14.3

The return value determines what happens next:

  • A character length identifies how many bytes were consumed. section 14.3
  • -1 signals an encoding error; -2 signals that len was insufficient. section 14.3

TAKEAWAY: len is a byte limit, not a promised character length. A wrapper named mbrtow starts with MB_LEN_MAX and increases the limit when mbrtowc returns -2, hiding that choice from its caller. A string-search function then uses the wrapper to advance through multibyte characters. section 14.3

14   size_t mbrtow(wchar_t*restrict C, char const c[restrict static 1],
15                 mbstate_t*restrict state) {
16     if (!state) state = MBSTATE;
17     size_t len = -2;
18     for (size_t maxlen = MB_LEN_MAX; len == -2; maxlen *= 2)
19       len = mbrtowc(C, c, maxlen, state);
20     if (len == -1) errno = 0;
21     return len;
22   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":12},{\"line\":4,\"ch\":18}],[{\"line\":0,\"ch\":19},{\"line\":0,\"ch\":26}],[{\"line\":0,\"ch\":27},{\"line\":0,\"ch\":35}],[{\"line\":1,\"ch\":29},{\"line\":1,\"ch\":37}],[{\"line\":0,\"ch\":39},{\"line\":0,\"ch\":49}],[{\"line\":0,\"ch\":52},{\"line\":0,\"ch\":67}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":19},{\"line\":1,\"ch\":28}],[{\"line\":0,\"ch\":27},{\"line\":0,\"ch\":35}],[{\"line\":1,\"ch\":29},{\"line\":1,\"ch\":37}],[{\"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\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":12},{\"line\":4,\"ch\":18}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":10}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":11}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":13}],[{\"line\":4,\"ch\":12},{\"line\":4,\"ch\":18}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":38}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":5,\"ch\":15},{\"line\":5,\"ch\":22}],[{\"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\":22},{\"line\":6,\"ch\":27}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the wrapper’s loop to see how a -2 result leads to a larger byte limit. section 14.3

How Modern C covers it

How deep it goes
Explains the byte limit, conversion state, and key return values through a wrapper; leaves encoding details machine-dependent.
Examples
Traces a wrapper that enlarges the byte limit after -2 and feeds a wide-character string search.
What you can do after
Apply it to convert successive multibyte characters, handling insufficient input separately from encoding errors.
How it connects
Connects multibyte strings, wide characters, conversion state, MB_LEN_MAX, and string search.

Also explained in

Compare how 2 books explain mbrtowc →

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.