Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char val = 'abcd'. Using multi character char

Tags:

c++

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.

like image 941
madu Avatar asked Dec 21 '14 14:12

madu


People also ask

Can char have multiple characters?

Also, there is a multi-character literal that contains more than one c-char. A single c-char literal has type char and a multi-character literal is conditionally-supported, has type int, and has an implementation-defined value.

How do you use a multi-character constant?

Multi-character constants can consist of as many as four characters. For example, the constant '\006\007\008\009' is valid only in a C++Builder program. Multi-character constants are always 32-bit int values. The constants are not portable to other C++ compilers.

Can chars store multiple characters in Java?

If you intended to print out abcdefghijklmnopqrstuvwxy, then you should have stored it into a string variable instead of a char one (char ch[50] = char abcdefghijklmnopqrstuvwxy;). String variables can hold more than one character, where as a char variable is for holding one character.

What is multi-character constant error in C++?

W8098 Multi-character character constant (C++)This warning is issued when the compiler detects a multi-character integer constant, such as: int foo = 'abcd'; The problem with this construct is that the byte order of the characters is implementation dependent.


1 Answers

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

like image 148
Columbo Avatar answered Oct 06 '22 01:10

Columbo