Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a confusion of how the compiler handles a char variable with multiple characters. I understand that a char is 1 byte and it can contain one character like ASCII.

But when I try:

char _val = 'ab';
char _val = 'abc';
char _val = 'abcd';

They compiles fine and when I print _val it always prints the last character. But when I did

char _val = 'abcde';

Then I got a compiler error:

Error 1 error C2015: too many characters in constant

So my questions are:

  1. Why does the compiler always takes the last character when multiple characters are used? What is the compiler mechanism in this situation.
  2. Why did I get a too many characters error when I put 5 characters. 2 characters is more than what a char can handle so why 5?

I am using Visual Studio 2013.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
596 views
Welcome To Ask or Share your Answers For Others

1 Answer

[lex.ccon]/1:

An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal [..] is conditionally-supported, has type int, and has an implementation-defined value.


Why does the compiler always takes the last character when multiple characters are used? What is the compiler mechanism in this situation.

Most compilers just shift the character values together in order: That way the last character occupies the least significant byte, the penultimate character occupies the byte next to the least significant one, and so forth.
I.e. 'abc' would be equivalent to 'c' + ((int)'b')<<8) + (((int)'a')<<16) (Demo).

Converting this int back to a char will have an implementation defined value - that might just emerge from taking the value of the int modulo 256. That would simply give you the last character.

Why did I get a too many characters error when I put 5 characters. 2 characters is more than what a char can handle so why 5?

Because on your machine an int is probably four bytes large. If the above is indeed the way your compiler arranges multicharacter constants in, he cannot put five char values into an int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...