liveBook · Manning

Mbrtowc

As explained in Modern C, Third Edition

mbrtowc is a C standard-library conversion function that converts a multibyte character sequence to a wide character while maintaining restartable conversion state.

mbrtowc converts one multibyte character from s into the wide character pointed to by pwc, using at most len bytes and preserving conversion state through mbstate_t. section 14.3

Interface

size_t mbrtowc(wchar_t* restrict pwc, const char* restrict s, size_t len, mbstate_t* restrict ps); section 14.3

Interpret the result together with the scan limit:

  • Use len as the maximum number of bytes examined for one multibyte character. section 14.3
  • Treat mbincomplete as an indication that len was too small for the character. section 14.3
  • Treat mbinvalid as a conversion error; invalid encoding can also set errno to EILSEQ. section 14.3
  • Pass conversion state when processing characters across calls; a wrapper can start with MB_LEN_MAX and enlarge len after mbincomplete. section 14.3
 size_t mbrtowc(wchar_t*restrict pwc,
                const char*restrict s, size_t len,
                mbstate_t* restrict ps);

Browse the exact C declaration for the conversion function. section 14.3

How Modern C, Third Edition covers it

How deep it goes
Explains the signature, scan limit, incomplete result, and adaptive-length wrapper without detailing encoding algorithms.
Examples
Uses a mbrtow wrapper that retries with increasing limits and reports mbinvalid after conversion failure.
What it assumes
Assumes familiarity with C pointers, wchar_t, multibyte strings, size_t, and conversion state.
What you can do after
Apply mbrtowc to inspect multibyte input incrementally and respond to incomplete or invalid characters.
How it connects
Connects multibyte character sequences with wide characters, mbstate_t, mbsrtowcs, MB_LEN_MAX, and errno.

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, Third Edition. The section links point to the parts of the book it draws on.