Multicharacter literal in C and C++

前端 未结 6 875
别那么骄傲
别那么骄傲 2020-11-22 03:30

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!)

6条回答
  •  旧时难觅i
    2020-11-22 04:09

    Multicharacter literals allow one to specify int values via the equivalent representation in characters. Useful for enums, FourCC codes and tags, and non-type template parameters. With a multicharacter literal, a FourCC code can be typed directly into the source, which is handy.

    The implementation in gcc is described at https://gcc.gnu.org/onlinedocs/cpp/Implementation-defined-behavior.html . Note that the value is truncated to the size of the type int, so 'efgh' == 'abcdefgh' if your ints are 4 chars wide, although gcc will issue a warning on the literal that overflows.

    Unfortunately, gcc will issue a warning on all multi-character literals if -pedantic is passed, as their behavior is implementation-defined. As you can see above, it is perhaps possible for equality of two multi-character literals to change if you switch implementations.

提交回复
热议问题