AndRAM AndRAM - 11 months ago 55
C Question

Are multi-character character constants valid in C? Maybe in MS VC?

While reviewing some WINAPI code intended to compile in MS Visual C++, I found the following (simplified):

char buf[4];

// buf gets filled ...

switch ((buf[0] << 8) + buf[1]) {
case 'CT':
/* ... */

case 'SY':
/* ... */



Assuming 16 bit chars, I can understand why the shift of
and addition of
. What I don't gather is how the comparisons in the case clauses are intended to work.

I don't have access to Visual C++ and, of course, those yield
multi-character character constant [-Wmultichar]
warnings on gcc/MingW.

Answer Source

This is a non-portable way of storing more than one chars in one int. Finally, the comparison happens as the int values, as usual.

Note: consider concatenated representation of the ASCII values for each individual char as the final int value.

Following the wiki article, (emphasis mine)

[...] 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 an int is not specified, portable use of multi-character constants is difficult.

Related, C11, chapter §

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. [....]