这段代码可以在g++中正常工作和运行。我不知道为什么。它应该会给出一个错误。
#include <iostream>
using namespace std;
int main(){
int x=9;
int y=6;
//note that there is extra backslash in the end of if statement
if(x==y)\
{
cout<<"x=y"<<endl;
}
//note that there is extra backslash in the end of if statement
if(x!=y)\
{
cout<<"x!=y"<<endl;
}
return 0;
}发布于 2012-10-16 04:16:45
来自C++标准:
(C++11,2.2p1)“删除紧跟换行符的每个反斜杠字符()实例,拼接物理源行以形成逻辑源行。只有任何物理源行上的最后一个反斜杠才有资格成为此类拼接的一部分。”
C语言说的完全一样:
(C11,5.1.1.2翻译阶段p1)“删除紧跟换行符的每个反斜杠字符()实例,拼接物理源代码行以形成逻辑源代码行。”
所以:
if(x==y)\
{
cout<<"x=y"<<endl;
}实际上相当于:
if(x==y){
cout<<"x=y"<<endl;
}发布于 2012-10-16 04:16:36
\转义换行符。g++将在一行中读取if(x==y){,这不是一个语法错误。
https://stackoverflow.com/questions/12903294
复制相似问题