long x = '\xde\xad\xbe\xef'; // yes, single quotes
Surprisingly (to me at least), this is valid ISO 9899:1999 C. It compiles without warning under gcc with -Wall
, and a “multi-character character constant” warning with -pedantic
.
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.
A bit of experimentation shows that gcc stores the leftmost character in the literal as the most-significant byte in the integer. But is this a reliable behavior in other common compilers? It’s a cool little piece of esoteric C syntax, but I’m guessing nobody interested in portable code should be using it when an integer constant works just as well.
It’s portable on all little endian machine (x86 and some arm). Never have any issue, but a pesky warning when it’s included in C++ code.