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





