加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 任何人都可以解释为什么x显示值1而不是2

发布时间:2020-12-16 10:35:30 所属栏目:百科 来源:网络整理
导读:我正在努力提高我的编程技巧.程序的输出如下 “如果是1,我就在其他地方.” 我想知道背后的原因,为什么x值没有初始化为2而是显示1. #include iostreamusing namespace std;int main(){ if (false) { cout "I'm in if " endl; } else if (int x=2 true) { cout
我正在努力提高我的编程技巧.程序的输出如下
“如果是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;
}

解决方法

根据操作符的优先顺序,

if (int x=2 && true)

被解析为

if (int x = (2 && true))

所以x = true所以1.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读