Why does this code with ‘1234’ compile in C++?

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'

Leave a Comment