混乱在C赋值运算符的关联性 [英] Confusion with Associativity of Assignment operator in 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:
-
A == b
是0
-
0 == 0
是1
-
1
分配给A
a == b
is0
0 == 0
is1
1
is assigned toa
这篇关于混乱在C赋值运算符的关联性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!