Why does this compile:
char ch = '1234'; //no error
But not anything more than 4 char
s :
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?
C++ has something called "multicharacter literals".
'1234'
is an example of one. They have typeint
, and it is implementation-defined what value they have and how many characters they can contain.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:or:
It's a multicharacter literal, and has a type of
int
.