Irbis
Irbis

Reputation: 1501

c++ - too many characters in character constant

I have the following legacy code:

unsigned int code = 'abcd';

I need to add one character to the above code:

unsigned int code = 'abcd2';

but then I get the following error: too many characters in character constant. Why using unsigned long int type doesn't resolve that issue ? Is it possible to fix it or I should modify the code and use a char array ?

Upvotes: 2

Views: 1789

Answers (1)

eerorika
eerorika

Reputation: 238421

Why using unsigned long int type doesn't resolve that issue ?

Because the type of the variable has no effect on the type of the literal. The type of multi-char literal is int. There are no unsigned long multi-char literals.

Also, on some systems unsigned long has exactly as many bytes as int has.

Is it possible to fix it

There is no way to fit more characters in multi-char literal.

or I should modify the code and use a char array ?

If you need more characters, then yes.

Upvotes: 7

Related Questions