18

Why does this compile:

char ch = '1234'; //no error

But not anything more than 4 chars :

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 chars are represented using ASCII numbers?

Yu Hao
  • 115,525
  • 42
  • 225
  • 281
Memo
  • 449
  • 3
  • 10
  • 1
    Related: http://stackoverflow.com/q/7755202/335858 – Sergey Kalinichenko Oct 16 '13 at 02:14
  • What made you think that it shouldn't? – AnT Oct 16 '13 at 02:15
  • 2
    possible duplicate of [What do single quotes do in C++ when used on multiple characters?](http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters) – AnT Oct 16 '13 at 02:16
  • 1
    @AndreyT I didn't know about multi-character literals and thought the compiler will reject more than one char in single quotation. – Memo Oct 16 '13 at 02:21
  • There's no reason at all to use multi-character literals in C++11 because you can just write your own UDL. – Simple Oct 16 '13 at 10:21
  • There is no requirement that `char`s use ASCII numbers. Most platforms use ASCII, but there are other character encodings. – Pete Becker Oct 16 '13 at 13:29

2 Answers2

17

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.

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:

'1' + 256 * '2' + 256 * 256 * '3' + 256 * 256 * 256 * '4'

or:

'4' + 256 * '3' + 256 * 256 * '2' + 256 * 256 * 256 * '1'
Steve Jessop
  • 265,622
  • 35
  • 449
  • 690
16

It's a multicharacter literal, and has a type of 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 letter L, as in L’x’. A character literal that does not begin with L 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 type int and implementation-defined value.

Community
  • 1
  • 1
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
  • @soandos The digits are treated as normal characters `'1'`, `'2'`, etc. According to the standard, it has implementation-defined value, but normally the value is as @Steve Jessop says in his answer. – Yu Hao Oct 16 '13 at 03:02
  • 1
    The second part of the question: why is it an error at 5 or more characters? Is that implementation defined? – Yakk - Adam Nevraumont Oct 16 '13 at 12:56
  • @Yakk I think so. It's likely that 5 or more characters would hold a value bigger than a 32-bit `int`, and I tested under GCC, it's a warning, not an error. – Yu Hao Oct 16 '13 at 13:05