Multicharacter literal in C and C++

前端 未结 6 974
别那么骄傲
别那么骄傲 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条回答
  •  我寻月下人不归
    2020-11-22 04:24

    In C++14 specification draft N4527 section 2.13.3, entry 2:

    ... An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.

    Previous answers to your question pertained mostly on real machines that did support multicharacter literals. Specifically, on platforms where int is 4 bytes, four-byte multicharacter is fine and can be used for convenience, as per Ferrucio's mem dump example. But, as there is no guarantee that this will ever work or work the same way on other platforms, use of multicharacter literals should be deprecated for portable programs.

提交回复
热议问题