Single quotes vs. double quotes in C or C++

前端 未结 13 1195
别那么骄傲
别那么骄傲 2020-11-21 07:14

When should I use single quotes and double quotes in C or C++ programming?

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 08:03

    Some compilers also implement an extension, that allows multi-character constants. The C99 standard says:

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

    This could look like this, for instance:

    const uint32_t png_ihdr = 'IHDR';
    

    The resulting constant (in GCC, which implements this) has the value you get by taking each character and shifting it up, so that 'I' ends up in the most significant bits of the 32-bit value. Obviously, you shouldn't rely on this if you are writing platform independent code.

提交回复
热议问题