1

C++ seems to allow up to four characters to be held in single quotes, such as:

char c = 'abcd';

but at runtime, only the last value ('d') seems to be actually stored away. This behavior seems to happen for pairs of two, three, or four (at five the compiler finally calls uncle). But what's the deal with this design? I don't really see the logic in it.

sircodesalot
  • 11,231
  • 8
  • 50
  • 83
  • http://stackoverflow.com/questions/3960954/c-multicharacter-literal – chris Oct 09 '13 at 13:43
  • 4
    [Multicharacter literals](http://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters) – David G Oct 09 '13 at 13:43
  • Usually g++, but for this example I just used VC++ – sircodesalot Oct 09 '13 at 13:44
  • The reason you only get `d` is because you're converting the `int` value of the literal to `char`, discarding all but one byte of it. (Note that it's implementation-defined; on other platforms, you might get `a`, or something else entirely). – Mike Seymour Oct 09 '13 at 13:48
  • @MikeSeymour: Is it really implementation defined, or Undefined? – John Dibling Oct 09 '13 at 13:52
  • 1
    @JohnDibling Both the value of the literal, and the result of signed overflow when converting to a smaller integer type, are implementation-defined. (It's also implementation-defined whether `char` is signed or not; if not, then the conversion result is defined). – Mike Seymour Oct 09 '13 at 13:54

0 Answers0