character constant
From cppreference.com
Contents |
[edit] Syntax
' c-char '
|
(1) | ||||||||
u ' c-char ' (since C11)
|
(2) | ||||||||
U ' c-char ' (since C11)
|
(3) | ||||||||
L ' c-char '
|
(4) | ||||||||
' c-char-sequence '
|
(5) | ||||||||
where
- c-char is either
- a character from the basic source character set minus single-quote (
'
), backslash (\
), or the newline character. - escape sequence: one of special character escapes \' \" \? \\ \a \b \f \n \r \t \v, hex escapes \x... or octal escapes \... as defined in escape sequences.
-
universal character name, \u... or \U... as defined in escape sequences.
(since C99) - c-char-sequence is a sequence of two or more c-chars.
1) single-byte integer character constant, e.g. 'a' or '\n' or '\13'. Such constant has type int and a value equal to the representation of c-char in the execution character set as a value of type char mapped to int. If c-char is not representable as a single byte in the execution character set, the value is implementation-defined.2) 16-bit wide character constant, e.g. u'貓', but not u'🍌' (u'\U0001f34c'). Such constant has type char16_t and a value equal to the value of c-char in the 16-bit encoding produced by mbrtoc16 (normally UTF-16). If c-char is not representable or maps to more than one 16-bit character, the behavior is implementation-defined.3) 32-bit wide character constant, e.g. U'貓' or U'🍌'. Such constant has type char32_t and a value equal to the value of c-char in in the 32-bit encoding produced by mbrtoc32 (normally UTF-32). If c-char is not representable or maps to more than one 32-bit character, the behavior is implementation-defined.4) wide character constant, e.g. L'β' or L'貓. Such constant has type wchar_t and a value equal to the value of c-char in the execution wide character set (that is, the value that would be produced by mbtowc). If c-char is not representable or maps to more than one wide character (e.g. a non-BMP value on Windows where wchar_t is 16-bit), the behavior is implementation-defined .5) multicharacter constant, e.g. 'AB', has type int and implementation-defined value.[edit] Notes
Many implementations of multicharacter constants use the values of each char in the constant to initialize successive bytes of the resulting integer, in big-endian order, e.g. the value of '\1\2\3\4' is 0x1020304.
In C++, ordinary character constants have type char, rather than int.
Unlike integer constants, a character constant may have a negative value if char is signed: on such implementations '\xFF' is an int 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.
[edit] References
- C11 standard (ISO/IEC 9899:2011):
- 6.4.4.4 Character constants (p: 67-70)
- C99 standard (ISO/IEC 9899:1999):
- 6.4.4.4 Character constants (p: 59-61)
- C89/C90 standard (ISO/IEC 9899:1990):
- 3.1.3.4 Character constants
[edit] See also
C++ documentation for character literal
- a character from the basic source character set minus single-quote (