C和C ++中的反斜杠后的空格字符 [英] Whitespace character after backslash in C and 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. 每个反斜杠字符( \
)后紧跟一个新行字符被删除,拼接物理源行以形成逻辑源行。只有任何物理源行上的最后一个反斜杠才有资格作为这种拼接的一部分。因此,如果产生与通用字符名的语法相匹配的字符序列,则该行为是未定义的。如果源文件不为空,并且不以新行字符结束,或者在任何此类拼接发生之前以反斜杠字符开头的新行字符结束,则应处理该源文件,行符号附加到文件。
[...]
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.
[...]
(请注意,注释在阶段3之前不会被替换,因此在此代码中:
(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.)
这篇关于C和C ++中的反斜杠后的空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!