Jag*_*rla 3 c++ operator-precedence
我正在努力提高我的编程技巧.程序的输出下面是' 我在其他地方,如果1 '.我想知道背后的原因,为什么x值没有初始化为2而是显示1.
#include <iostream>
using namespace std;
int main()
{
if (false)
{
cout << "I'm in if " << endl;
}
else if (int x=2 && true)
{
cout << "I'm in else if " << x << endl;
}
else
{
int y = x;
cout << y << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据运营商的优先顺序,
if (int x=2 && true)
Run Code Online (Sandbox Code Playgroud)
被解析为
if (int x = (2 && true))
Run Code Online (Sandbox Code Playgroud)
所以x = true如此1.