C multi-character character constants

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.

5 thoughts on “C multi-character character constants”

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

Leave a Reply

Your email address will not be published. Required fields are marked *