liveBook · Manning

UTF-8

As explained in Modern C, Third Edition

UTF-8 is a variable-length Unicode encoding that represents each code point as a sequence of one to four bytes.

UTF-8 is a variable-length Unicode encoding: each code point occupies one to four bytes. In C, a UTF-8 literal can be written with the u8 prefix, as in u8"𝒜". section 14.4

A UTF-8 literal in C

u8"𝒜" has type char8_t[5] in the correspondence table: four bytes encode the character and one byte terminates the string. The u8 prefix is useful when the program must explicitly ensure UTF-8, such as in network communication. section 14.4

UTF-8 processing becomes stateful when input arrives in pieces. Repeated calls to mbrtoc8 use the same mbstate_t object to collect a complete multibyte sequence and write its UTF-8 bytes to an output buffer. section 14.5

A C scanner must account for these cases:

  • Treat mbincomplete as a sequence that may continue in the next input buffer. section 14.5
  • Treat mbinvalid as an input error rather than a completed code point. section 14.5

How Modern C, Third Edition covers it

How deep it goes
Explains literal types and incremental conversion, then stops short of a complete encoding algorithm.
Examples
Uses u8 literals, a UTF-8 correspondence table, and a C scanner built around mbrtoc8 and mbstate_t.
What it assumes
Assumes C string and I/O familiarity while teaching UTF literal types, locale setup, conversion state, and partial input.
What you can do after
Apply UTF-8 through C11 character literals and diagnose incomplete or invalid multibyte input with mbrtoc8.
How it connects
Links Unicode, UTF-16, UTF-32, multibyte encoding, locales, and restartable text conversion.

Also explained in

Compare how 11 books explain UTF-8 →

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.