gpt4 book ai didi

c++ - 额外的反斜杠字符不会影响我的程序。为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:09 30 4
gpt4 key购买 nike

这段代码可以在 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;
}

最佳答案

来自 C++ 标准:

(C++11, 2.2p1) "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."

C 说的完全一样:

(C11, 5.1.1.2 Translatation phases p1) "Each instance of a backslash character (\) immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines."

所以:

if(x==y)\
{
cout<<"x=y"<<endl;
}

实际上等同于:

if(x==y){
cout<<"x=y"<<endl;
}

关于c++ - 额外的反斜杠字符不会影响我的程序。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12903294/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com