在C和C反斜线后空白字符++

在C和C反斜线后空白字符++

问题描述:

C和C ++标准说什么反斜线后空白字符(或多个字符)?难道它保证反正还是不加入行?

What C and C++ standards says about whitespace character (or several characters) after backslash? Does it guarantees to join lines anyway or not?

int main()
{
    // Comment \ 
    int foo;
}

MSVC和GCC在这种情况下,不同的工作。

MSVC and gcc works different in this case.

有关参考,标准报价(§2.2/ 1,删节,重点煤矿):

For reference, the standard quote is (§2.2/1, abridged, emphasis mine):

[...]结果
  2. 反斜杠字符( \\ ),紧跟​​着一个换行符的每个实例被删除,拼接物理源线形成逻辑源代码行。只有在任何物理源行的最后一个反斜杠有资格成为这种接头的一部分。如果作为结果,产生了一个普遍的字符名称的语法相匹配的字符序列,该行为是不确定的。源文件,是不是空的,并且不以新行字符结束,或在新的行字符结束立即美元的任何此类拼接发生之前一个反斜杠字符pceded p $,应处理,如同一个额外的换行符被附加到文件。结果
  [...]

Phases of Translation

[...]
2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice. If, as a result, a character sequence that matches the syntax of a universal-character-name is produced, the behavior is undefined. A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file.
[...]

实现定义的一部分,其他答案提的是,在新线的定义。

The implementation-defined part that other answers are mentioning is in the definition of "new-line".

(注意:评论不会被替换,直到第3阶段,所以在这个code:

(Note that comments are not replaced until phase 3, so that in this code:

int main()
{
    int x = 0;

    // assuming the definition of new-line is as expected, this function
    // will return 0, not 5 (no whitespace after this backslash: ) \
    x = 5;

    return x;
}

X = 5; 将被追加到的意见,然后最终删除月底)

x = 5; will be appended to the end of the comment, then ultimately removed.)