Compilation of string literals

后端 未结 5 1223
臣服心动
臣服心动 2021-01-13 16:32

Why can two string literals separated by a space, tab or \"\\n\" be compiled without an error?

int main()
{
   char * a = \"aaaa\"  \"bbbb\";
} 
5条回答
  •  生来不讨喜
    2021-01-13 17:22

    Adjacent string literals are concatenated as per the rules of C (and C++) standard. But no such rule exists for adjacent identifiers (i.e. variables a and b).

    To quote, C++14 (N3797 draft), § 2.14.5:

    In translation phase 6 (2.2), adjacent string literals are concatenated. If both string literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand. If a UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other concatenations are conditionally-supported with implementation-defined behavior.

提交回复
热议问题