Multicharacter literal in C and C++

Viewed 12435

I didn't know that C and C++ allow multicharacter literal: not 'c' (of type int in C and char in C++), but 'tralivali' (of type int!)

enum
{
    ActionLeft = 'left',
    ActionRight = 'right',
    ActionForward = 'forward',
    ActionBackward = 'backward'
};

Standard says:

C99 6.4.4.4p10: "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."

I found they are widely used in C4 engine. But I suppose they are not safe when we are talking about platform-independend serialization. Thay can be confusing also because look like strings. So what is multicharacter literal's scope of usage, are they useful for something? Are they in C++ just for compatibility with C code? Are they considered to be a bad feature as goto operator or not?

6 Answers

unbelievable, every compiler I know places the first character of a UINT defined as 4-character constant in the low significant byte (little indian) --- but Visual C does it in opposite direction

// file signature
#define SFKFILE_SIGNATURE 'SFPK' (S=53)

// check header
if (out_FileHdr->Signature != SFKFILE_SIGNATURE)
  • fails on VC:

Borland: 4B504653 4B504653

Watcom: 4B504653 4B504653

VisualC: 4B504653 5346504B

Related