' c-char ' | (1) | |
u ' c-char ' (since C11) | (2) | |
U ' c-char ' (since C11) | (3) | |
L ' c-char ' | (4) | |
' c-char-sequence ' | (5) |
where.
'
), backslash (\
), or the newline character. \'
\"
\?
\\
\a
\b
\f
\n
\r
\t
\v
, hex escapes \x...
or octal escapes \...
as defined in escape sequences.
| (since C99) |
'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.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.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.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 .'AB'
, has type int
and implementation-defined value.Multicharacter constants were inherited by C from the B programming language. Although not specified by the C standard, compilers 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'
is 0x00000001
and the value of '\1\2\3\4'
is 0x01020304
.
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.
#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': %#010x\n", c1); int c2='๐'; printf("'๐': %#010x\n\n", c2); // implementation-defined // multicharacter constant int c3='ab'; printf("'ab': %#010x\n\n", c3); // implementation-defined // 16-bit wide character constants char16_t uc1 = u'a'; printf("'a': %#010x\n", (int)uc1); char16_t uc2 = u'ยข'; printf("'ยข': %#010x\n", (int)uc2); char16_t uc3 = u'็ซ'; printf("'็ซ': %#010x\n", (int)uc3); // implementation-defined (๐ maps to two 16-bit characters) char16_t uc4 = u'๐'; printf("'๐': %#010x\n\n", (int)uc4); // 32-bit wide character constants char32_t Uc1 = U'a'; printf("'a': %#010x\n", (int)Uc1); char32_t Uc2 = U'ยข'; printf("'ยข': %#010x\n", (int)Uc2); char32_t Uc3 = U'็ซ'; printf("'็ซ': %#010x\n", (int)Uc3); char32_t Uc4 = U'๐'; printf("'๐': %#010x\n\n", (int)Uc4); // wide character constants wchar_t wc1 = L'a'; printf("'a': %#010x\n", (int)wc1); wchar_t wc2 = L'ยข'; printf("'ยข': %#010x\n", (int)wc2); wchar_t wc3 = L'็ซ'; printf("'็ซ': %#010x\n", (int)wc3); wchar_t wc4 = L'๐'; printf("'๐': %#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
ยฉ cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/language/character_constant