Multi-character constant warnings

C++CCastingCompiler WarningsPortability

C++ Problem Overview


Why is this a warning? I think there are many cases when is more clear to use multi-char int constants instead of "no meaning" numbers or instead of defining const variables with same value. When parsing wave/tiff/other file types is more clear to compare the read values with some 'EVAW', 'data', etc instead of their corresponding values.

Sample code:

int waveHeader = 'EVAW';

Why does this give a warning?

C++ Solutions


Solution 1 - C++

According to the standard (§6.4.4.4/10)

> The value of an integer character constant containing more than one > character (e.g., 'ab'), [...] is implementation-defined.

long x = '\xde\xad\xbe\xef'; // yes, single quotes

This is valid ISO 9899:2011 C. It compiles without warning under gcc with -Wall, and a “multi-character character constant” warning with -pedantic.

From Wikipedia:

> Multi-character constants (e.g. 'xy') are valid, although rarely > useful — they let one store several characters in an integer (e.g. 4 > ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one). > Since the order in which the characters are packed into one int is not > specified, portable use of multi-character constants is difficult.

For portability sake, don't use multi-character constants with integral types.

Solution 2 - C++

This warning is useful for programmers that would mistakenly write 'test' where they should have written "test".

This happen much more often than programmers that do actually want multi-char int constants.

Solution 3 - C++

If you're happy you know what you're doing and can accept the portability problems, on GCC for example you can disable the warning on the command line:

-Wno-multichar

I use this for my own apps to work with AVI and MP4 file headers for similar reasons to you.

Solution 4 - C++

Even if you're willing to look up what behavior your implementation defines, multi-character constants will still vary with endianness.

Better to use a (POD) struct { char[4] }; ... and then use a UDL like "WAVE"_4cc to easily construct instances of that class

Solution 5 - C++

Simplest C/C++ any compiler/standard compliant solution, was mentioned by @leftaroundabout in comments above:

int x = *(int*)"abcd";

Or a bit more specific:

int x = *(int32_t*)"abcd";

One more solution, also compliant with C/C++ compiler/standard since C99 (except clang++, which has a known bug):

int x = ((union {char s[5]; int number;}){"abcd"}).number;

/* just a demo check: */
printf("x=%d stored %s byte first\n", x, x==0x61626364 ? "MSB":"LSB");

Here anonymous union is used to give a nice symbol-name to the desired numeric result, "abcd" string is used to initialize the lvalue of compound literal (C99).

Solution 6 - C++

If you want to disable this warning it is important to know that there are two related warning parameters in GCC and Clang: https://stackoverflow.com/q/47304722/4915707

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMircea IspasView Question on Stackoverflow
Solution 1 - C++user195488View Answer on Stackoverflow
Solution 2 - C++Didier TrossetView Answer on Stackoverflow
Solution 3 - C++blueshiftView Answer on Stackoverflow
Solution 4 - C++o11cView Answer on Stackoverflow
Solution 5 - C++sqr163View Answer on Stackoverflow
Solution 6 - C++Alexander UshakovView Answer on Stackoverflow