liveBook · Manning

Unicode

As explained in Modern C, Third Edition

Unicode is a universal character encoding standard that assigns code points and properties to characters and symbols across writing systems.

Characters in C source

`long année = 1990L; // Année de l'écriture de l'œuvre` uses non-ASCII characters in an identifier and a comment. Such code can be appropriate when the project's language suits its participants; difficulty accepting it may instead point to an environment or implementation limitation. section 9.3

Unicode, aligned here with ISO 10646, assigns code points to character concepts rather than to particular printed shapes. The same letter can have several glyphs, while characters from different scripts can look alike. Unicode also classifies characters and records relationships such as uppercase and lowercase forms. section 14.3

In C, distinguish the character from its representation:

  • A multibyte string stores encoded bytes. Neither its array size nor `strlen` generally gives its number of characters. Parsing may require an `mbstate_t` conversion state and facilities from `<wchar.h>`. section 14.3
  • A source file can contain a character directly, or use a universal character escape such as `\u2502` for a vertical bar. Whether direct multibyte text works as intended depends on the environment. section 14.3
  • UTF-8, UTF-16 and UTF-32 are Unicode encodings. C provides prefixed literals such as `u8"text"`, `u"text"` and `U"text"` when an encoding must be specified rather than left to the locale-dependent environment. section 14.4
Table 14.4 Common correspondence between UTF and basic character types
Literal typedef Type Encoding Correspondence
u8’a’ uchar8_t unsigned char UTF-8 ASCII in char
u’π’ uchar16_t uint_least16_t UTF-16
U’𝒜’ uchar32_t uint_least32_t UTF-32 " wc"  in wchar_t
u8"𝒜" uchar8_t[5] unsigned char[5] UTF-8 " mbs"  in char[]
u"𝒜" uchar16_t[3] uint_least16_t[3] UTF-16
U"𝒜" uchar32_t[2] uint_least32_t[2] UTF-32 " wcs"  in wchar_t[]

The table compares UTF literal prefixes, element types and encodings. Its `𝒜` strings also show why one character does not always occupy one string element: UTF-16 uses two words for that code point, plus a terminating null element. section 14.4

TAKEAWAY: Do not equate a Unicode code point, a glyph, a byte or a C string element. Check the encoding used by source files, text files and terminals before moving text between environments; they are not guaranteed to agree. section 14.3 section 14.4

How Modern C, Third Edition covers it

How deep it goes
Defines code points and properties, then works through multibyte conversion concerns and UTF literals without detailing each encoding's bit layout.
Examples
Uses a French C identifier, Unicode escapes, UTF literal comparisons and a UTF-16 surrogate-pair example.
What it assumes
Assumes familiarity with C strings and literals but introduces Unicode code points and their distinction from glyphs.
What you can do after
Apply it via C literals and conversion facilities while checking character counts and encoding assumptions.
How it connects
Connects Unicode to ISO 10646, glyphs, multibyte strings, locales, wide characters and UTF encodings.

Also explained in

Compare how 8 books explain Unicode →

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.