Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

c++ - char val = 'abcd'. Using multi character char

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

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

1 Reply

0 votes
by (71.8m points)

[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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...