Why u8'A' can be a char type while UTF-8 can be up to 4 bytes and char is normally 1 byte?

  • A+
Category:Languages

I was reading What is the use of wchar_t in general programming? and found something confusing in the accepted answer:

It's more common to use char with a variable-width encoding e.g. UTF-8 or GB 18030.

And I find this from my textbook:

Why u8'A' can be a char type while UTF-8 can be up to 4 bytes and char is normally 1 byte?

Isn't Unicode encoding with UTF-8 is at most 4 bytes? char for most platforms is 1 byte. Do I misunderstand something?


Update:

After searching and reading, now I know that:

  1. code points and code units are different stuff. Code point is unique while code units rely on encoding.
  2. u8'a'(a char, not string here) is only allowed for basic character set(the ASCII and it's control character stuff), and its value is the corresponding 'a''s code unit value, and for ascii characters, code units are same value as code points. (This is what @codekaizer's answer say)
  3. std::string::size() returns code units.

New question:

  1. This character ə is encoded with 2 code units (from this website https://unicode-table.com/en/0259/). Then I tried string s="ə";cout<<s.size(); and the output is 1, how come?
  2. So the editors are all dealing with code units right? And If I change my file encoding from utf8 to uft32, then size of ə would be 4?

Isn't unicode encoding with utf8 is at most 4 bytes?

As per lex.ccon/3, emphasis mine:

A character literal that begins with u8, such as u8'w', is a character literal of type char, known as a UTF-8 character literal. The value of a UTF-8 character literal is equal to its ISO 10646 code point value, provided that the code point value is representable with a single UTF-8 code unit (that is, provided it is in the C0 Controls and Basic Latin Unicode block). If the value is not representable with a single UTF-8 code unit, the program is ill-formed. A UTF-8 character literal containing multiple c-chars is ill-formed.

Single UTF-8 code unit is 1 byte.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: