Why does this compile:
为什么编译:
char ch = '1234'; //no error
But not anything more than 4 char
s :
但不超过4个字符:
char ch = '12345'; //error: Too many chars in constant
(Yes I know ' '
is used for one char
and " "
is for strings; I was just experimenting)
(是的,我知道''用于一个字符串,“”用于字符串;我只是在尝试)
Does this have anything to do with the fact that char
s are represented using ASCII numbers?
这是否与使用ASCII数字表示字符的事实有关?
2 个解决方案
#1
15
It's a multicharacter literal, and has a type of int
.
它是一个多字符文字,并且有一种int类型。
C++11 §2.13.2 Character literals
A character literal is one or more characters enclosed in single quotes, as in
’x’
, optionally preceded by the letterL
, as inL’x’
. A character literal that does not begin withL
is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has typeint
and implementation-defined value.字符文字是用单引号括起来的一个或多个字符,如'x',可选地以字母L开头,如'x'中所示。不以L开头的字符文字是普通字符文字,也称为窄字符文字。包含单个c-char的普通字符文字具有char类型,其值等于执行字符集中c-char的编码的数值。包含多个c-char的普通字符文字是多字符文字。多字符文字具有int类型和实现定义的值。
#2
16
C++ has something called "multicharacter literals". '1234'
is an example of one. They have type int
, and it is implementation-defined what value they have and how many characters they can contain.
C ++有一种称为“多字符文字”的东西。 '1234'是一个例子。它们具有int类型,并且它是实现定义的,它们具有什么值以及它们可以包含多少个字符。
It's nothing directly to do with the fact that characters are represented as integers, but chances are good that in your implementation the value of '1234'
is defined to be either:
这与字符表示为整数这一事实没有直接关系,但在您的实现中,'1234'的值被定义为:
'1' + 256 * '2' + 256 * 256 * '3' + 256 * 256 * 256 * '4'
or:
'4' + 256 * '3' + 256 * 256 * '2' + 256 * 256 * 256 * '1'
#1
15
It's a multicharacter literal, and has a type of int
.
它是一个多字符文字,并且有一种int类型。
C++11 §2.13.2 Character literals
A character literal is one or more characters enclosed in single quotes, as in
’x’
, optionally preceded by the letterL
, as inL’x’
. A character literal that does not begin withL
is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has typeint
and implementation-defined value.字符文字是用单引号括起来的一个或多个字符,如'x',可选地以字母L开头,如'x'中所示。不以L开头的字符文字是普通字符文字,也称为窄字符文字。包含单个c-char的普通字符文字具有char类型,其值等于执行字符集中c-char的编码的数值。包含多个c-char的普通字符文字是多字符文字。多字符文字具有int类型和实现定义的值。
#2
16
C++ has something called "multicharacter literals". '1234'
is an example of one. They have type int
, and it is implementation-defined what value they have and how many characters they can contain.
C ++有一种称为“多字符文字”的东西。 '1234'是一个例子。它们具有int类型,并且它是实现定义的,它们具有什么值以及它们可以包含多少个字符。
It's nothing directly to do with the fact that characters are represented as integers, but chances are good that in your implementation the value of '1234'
is defined to be either:
这与字符表示为整数这一事实没有直接关系,但在您的实现中,'1234'的值被定义为:
'1' + 256 * '2' + 256 * 256 * '3' + 256 * 256 * 256 * '4'
or:
'4' + 256 * '3' + 256 * 256 * '2' + 256 * 256 * 256 * '1'