混乱在C赋值运算符的关联性 [英] Confusion with Associativity of Assignment operator in C

查看:155
本文介绍了混乱在C赋值运算符的关联性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都知道,赋值运算符的结合性是从右到左,但
在给定的code输出应该是零,如果我们从右到左但输出为1。

As we all know that Associativity of assignment operator is from right to left but in the given code output should be zero if we go from right to left but output is 1 .

 main()
 {
 int a=3,b=2;
 a=a==b==0;
 printf("%d",a);
 }

如何输出现身为1,如果我们去了右letf ??

How output is coming out to be 1 if we go by right to letf??

如果我们去了从右到左,那么(B == 0)应先评估并给出结果0,然后前pression(A == 0)进行了评价给出了0,最后一个值将是0。

If we go by right to left then (b==0) should be Evaluated first and gives result 0 and then expression (a==0) is Evaluated also gives 0 and at last a's value will be 0.

推荐答案

转让完成RTL,但平等( == )不是。

Assignment is done RTL, but equality (==) isn't.

该声明实际上是:

a = ((a == b) == 0)

分配的右手边是从左至右进行评价。在步骤,这是发生了什么:

The right hand side of the assignment is evaluated from left to right. In steps, this is what's happening:


  1. A == b 0

  2. 0 == 0 1

  3. 1 分配给 A

  1. a == b is 0
  2. 0 == 0 is 1
  3. 1 is assigned to a

这篇关于混乱在C赋值运算符的关联性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆