Contents |
'c-char' | (1) | ||||||||
u8'c-char' | (2) | (since C23) | |||||||
u'c-char' | (3) | (since C11) | |||||||
U'c-char' | (4) | (since C11) | |||||||
L'c-char' | (5) | ||||||||
'c-char-sequence' | (6) | ||||||||
L'c-char-sequence' | (7) | ||||||||
u'c-char-sequence' | (8) | (since C11)(removed in C23) | |||||||
U'c-char-sequence' | (9) | (since C11)(removed in C23) | |||||||
where
'), backslash (\), or the newline character.
| (since C99) |
3) 16-bit wide character constant, e.g.u'貓', but notu'🍌' (u'\U0001f34c'). Such constant has typechar16_t and a value equal to the value ofc-char in the 16-bit encoding produced bymbrtoc16 (normally UTF-16). Ifc-char is not representable or maps to more than one 16-bit character, the value is implementation-defined. 4) 32-bit wide character constant, e.g.U'貓' orU'🍌'. Such constant has typechar32_t and a value equal to the value ofc-char in in the 32-bit encoding produced bymbrtoc32 (normally UTF-32). Ifc-char is not representable or maps to more than one 32-bit character, the value is implementation-defined. | (until C23) |
3) UTF-16 character constant, e.g.u'貓', but notu'🍌' (u'\U0001f34c'). Such constant has typechar16_t and the value equal to ISO 10646 code point value ofc-char, provided that the code point value is representable with a single UTF-16 code unit (that is,c-char is in the range 0x0-0xD7FF or 0xE000-0xFFFF, inclusive). Ifc-char is not representable with a single UTF-16 code unit, the program is ill-formed. 4) UTF-32 character constant, e.g.U'貓' orU'🍌'. Such constant has typechar32_t and the value equal to ISO 10646 code point value ofc-char, provided that the code point value is representable with a single UTF-32 code unit (that is,c-char is in the range 0x0-0xD7FF or 0xE000-0x10FFFF, inclusive). Ifc-char is not representable with a single UTF-32 code unit, the program is ill-formed. | (since C23) |
Multicharacter constants were inherited by C from the B programming language. Although not specified by the C standard, most compilers (MSVC is a notable exception) implement multicharacter constants as specified in B: the values of each char in the constant initialize successive bytes of the resulting integer, in big-endian zero-padded right-adjusted order, e.g. the value of'\1' is0x00000001 and the value of'\1\2\3\4' is0x01020304.
In C++, encodable ordinary character literals have typechar, rather thanint.
Unlikeinteger constants, a character constant may have a negative value ifchar is signed: on such implementations'\xFF' is anint with the value-1.
When used in a controlling expression of#if or#elif, character constants may be interpreted in terms of the source character set, the execution character set, or some other implementation-defined character set.
16/32-bit multicharacter constants are not widely supported and removed in C23. Some common implementations (e.g. clang) do not accept them at all.
#include <stddef.h>#include <stdio.h>#include <uchar.h> int main(void){printf("constant value\n");printf("-------- ----------\n"); // integer character constants,int c1='a';printf("'a':\t %#010x\n", c1);int c2='🍌';printf("'🍌':\t %#010x\n\n", c2);// implementation-defined // multicharacter constantint c3='ab';printf("'ab':\t %#010x\n\n", c3);// implementation-defined // 16-bit wide character constants char16_t uc1= u'a';printf("'a':\t %#010x\n",(int)uc1); char16_t uc2= u'¢';printf("'¢':\t %#010x\n",(int)uc2); char16_t uc3= u'猫';printf("'猫':\t %#010x\n",(int)uc3);// implementation-defined (🍌 maps to two 16-bit characters) char16_t uc4= u'🍌';printf("'🍌':\t %#010x\n\n",(int)uc4); // 32-bit wide character constants char32_t Uc1= U'a';printf("'a':\t %#010x\n",(int)Uc1); char32_t Uc2= U'¢';printf("'¢':\t %#010x\n",(int)Uc2); char32_t Uc3= U'猫';printf("'猫':\t %#010x\n",(int)Uc3); char32_t Uc4= U'🍌';printf("'🍌':\t %#010x\n\n",(int)Uc4); // wide character constantswchar_t wc1= L'a';printf("'a':\t %#010x\n",(int)wc1);wchar_t wc2= L'¢';printf("'¢':\t %#010x\n",(int)wc2);wchar_t wc3= L'猫';printf("'猫':\t %#010x\n",(int)wc3);wchar_t wc4= L'🍌';printf("'🍌':\t %#010x\n\n",(int)wc4);}
Possible output:
constant value -------- ----------'a': 0x00000061'🍌': 0xf09f8d8c 'ab': 0x00006162'a': 0x00000061'¢': 0x000000a2'猫': 0x0000732b'🍌': 0x0000df4c 'a': 0x00000061'¢': 0x000000a2'猫': 0x0000732b'🍌': 0x0001f34c'a': 0x00000061'¢': 0x000000a2'猫': 0x0000732b'🍌': 0x0001f34c
C++ documentation forCharacter literal |