liveBook · Manning

Code point

As explained in Modern C, Third Edition

A code point is a numeric value identifying a character or abstract symbol in a character repertoire such as Unicode.

Consider `#define HBAR "\u2500"`: the escape names a Unicode code point for a horizontal bar. A code point identifies a character concept, not a particular drawing of it or the bytes used to store it. One letter can have several printed forms; distinct letters can even look alike. section 14.3

 #define VBAR "\u2502"      /**< a vertical bar character   */
 #define HBAR "\u2500"      /**< a horizontal bar character */
 #define TOPLEFT "\u250c"   /**< topleft corner character   */
 #define TOPRIGHT "\u2510"  /**< topright corner character  */

The four `\u` escapes name box-drawing characters without requiring those characters on the keyboard. section 14.3

In C, `\u` followed by four hexadecimal digits specifies a code point; `\U` followed by eight digits provides a longer form. Thus `"\u00E4"` specifies the character concept “latin small letter a with diaeresis” inside a multibyte string. The resulting representation still depends on its character encoding. TAKEAWAY: a code point is not necessarily one byte or one string element. section 14.3 section 14.4

One code point, different element counts

The code point `0x1D49C` does not fit in one 16-bit element. The UTF-16 literal `u"𝒜"` therefore has three elements: a surrogate pair and a terminating null character. The corresponding UTF-32 literal `U"𝒜"` has two elements, including its terminator. Count encoded elements according to the encoding, not by counting visible symbols. 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[]

Compare the listed UTF-8, UTF-16, and UTF-32 literal lengths for the same character. section 14.4

Code points also matter when choosing identifiers. Unicode Normalization Form C decomposes and recomposes character sequences, so different-looking specifications can resolve to the same result. For alphabetic identifier letters, use only characters that map to themselves under Form C. Even then, normalization cannot distinguish lookalike glyphs from different languages, such as Latin A and Greek Alpha. section 9.3

For input, do not assume a read buffer ends at a code-point boundary. Restartable C conversion functions retain `mbstate_t` state until a multibyte character is complete; an incomplete input can be reported as `mbincomplete`. Set the character locale before interpreting locale-dependent multibyte input. TAKEAWAY: distinguish the code point being processed from the possibly partial sequence carrying it. section 14.5

How Modern C, Third Edition covers it

How deep it goes
Defines the character concept and works through escapes, UTF element counts, normalization, and incomplete multibyte input.
Examples
Uses box-drawing escapes, an accented letter, and UTF-16 and UTF-32 literals for `𝒜`.
What it assumes
Assumes familiarity with C strings and literals while defining Unicode's distinction between a character concept and its printed form.
What you can do after
Apply it via C escapes and restartable conversion to represent characters and recognize complete multibyte input.
How it connects
Connects code points to glyphs, Unicode normalization, C identifiers, UTF encodings, surrogate pairs, and restartable conversion.

Also explained in

Compare how 6 books explain code point →

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.