增量和逻辑运算符precedence [英] Increment and logical operators precedence
问题描述
在下面所示的程序,preFIX应首先评估,因为它具有更高的precedence,但答案是 -2,2,0,1
并且在书中解释说,因为||的LHS是真实的RHS不计算。结果
为什么会这样呢?所有单位应先进行,然后逻辑应该因为precedence进行检查。
#包括LT&;&stdio.h中GT;诠释的main()
{
INT I = -3,J = 2,K = 0,米;
M = ++我|| ++ J&安培;&安培; ++ K表;
的printf(%D,%D,%D,%d个\\ N,I,J,K,M);
返回0;
}
不要误会与评价的 precedence 和订单的
评估顺序逻辑的或的 ||
为从左到右。
所以,如果左= TRUE
然后左||权
将不会执行右键
。在您的code完全相同发生。
如你所知,任何非零值视为真正
在C,因此, ++我
或 -2
是真正
。因此,
M = ++我|| ++ J&安培;&安培; ++ K表;
M =真||唧唧歪歪; //权甚至没有检查!
M =真
m = 1时
和你预期的输出。
In the program shown below, prefix should be evaluated first because it has higher precedence, But answer is -2, 2, 0, 1
and it is explained in book "as LHS of || is true RHS is not evaluated."
Why is it so? All the increments should performed first and then logical should be checked because of precedence.
#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
Don't get confused with Precedence and Order of evaluation.
The order of evaluation of logical OR ||
is left to right.
So if left = true
then left || right
will never execute right
. In your code exactly same happened.
As you know, any non zero value treated as true
in C, hence, ++i
or -2
is true
. So,
m = ++i || ++j && ++k;
m = true || bla bla bla; //right not even checked!
m = true
m = 1
And you get the output as expected.
这篇关于增量和逻辑运算符precedence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!