[英]How this logical expression is evaluating
#include<stdio.h>
int main()
{
int a=-10,b=3,c=0,d;
d= a++||++b &&c++;
printf("%d %d %d %d ",a,b,c,d);
}
How above expression is evaluates.上面的表达式是如何计算的。 I have read preceedence but still i am getting confused.我已经阅读了preceedence,但我仍然感到困惑。 Kindly give me the right solution of this in step by step.请逐步给我正确的解决方案。
In case of ||
在||
的情况下and &&
operators the order of evaluation is indeed defined as left-to-right.So, operator precedence rules tell you that the grouping should be和&&
运算符的评估顺序确实定义为从左到右。因此,运算符优先级规则告诉您分组应该是
(a++) || ((++b) && (c++))
Now, order-of-evaluation rules tell you that first we evaluate a++
, then (if necessary) we evaluate ++b
, then (if necessary) we evaluate c++
, then we evaluate &&
and finally we evaluate ||
现在,评估顺序规则告诉您,首先我们评估a++
,然后(如有必要)我们评估++b
,然后(如有必要)我们评估c++
,然后我们评估&&
最后我们评估||
. .
EDIT: The question was originally tagged C++ so I wrote this answer for C++.编辑:这个问题最初被标记为 C++ 所以我为 C++ 写了这个答案。
I have a feeling this is a homework question, so I'll try to give extra explanation.我觉得这是一个家庭作业问题,所以我会尝试给出额外的解释。 There are actually a lot of concepts going on here!这里实际上有很多概念!
Here are the main concepts:以下是主要概念:
++a
increments a
before the value is used in an expression, while a++
increments a
after the value is used in the expression. ++a
在值用于表达式之前递增a
,而a++
在值用于表达式之后递增a
。&&
has higher precedence than ||
具体来说, &&
的优先级高于||
, which means that the expression assigned to d
should be should be read as d = (a++) || (++b && c++);
,这意味着分配给d
的表达式应该读作d = (a++) || (++b && c++);
d = (a++) || (++b && c++);
&&
and ||
这就是说,对于运算符&&
和||
, if the value of the left-hand term is enough to determine the result of the expression, then the right-hand term is not evaluated. ,如果左项的值足以确定表达式的结果,则不计算右项。||
特别是,第 6 项说(释义) ||
的左侧 arguments and &&
are always evaluated before right-hand arguments, provided those operators are not overridden.和&&
总是在右手 arguments 之前评估,前提是这些运算符没有被覆盖。int
is cast to a bool
, a nonzero value becomes true
, while 0
becomes false
.在 C++ 中,如果将int
强制转换为bool
,则非零值变为true
,而0
变为false
。 When converting the other way, false
becomes 0
and true
becomes 1
.当以另一种方式转换时, false
变为0
并且true
变为1
。Putting this all together, here is what happens:把这一切放在一起,这就是发生的事情:
d
needs to be assigned.接下来,需要分配变量d
。 To determine the value assigned to d
, the left hand term of (a++) || (++b && c++)
为了确定分配给d
的值, (a++) || (++b && c++)
(a++) || (++b && c++)
is evaluated, which is a++
. (a++) || (++b && c++)
被评估,它是a++
。 The value USED in the expression is 10, however the value of a
after this expression is -9 due to the post-increment.表达式中 USED 的值为 10,但是由于后自增,该表达式后面的a
的值为 -9。10
becomes true
.当转换为 boolean 时,值10
变为true
。 This means that the value of the ||
这意味着||
的值expression must be true
.表达式必须为true
。 Because of short-circuit evaluation, this means that ++b && c++
is not evaluated at all, so the increments do not happen.由于短路评估,这意味着根本不评估++b && c++
,因此不会发生增量。d
is true
, but cast to an int
.要分配给d
的值是true
,但要转换为int
。 The result is 1, so we have d = 1
.结果是 1,所以我们有d = 1
。 So the program prints out -9 3 0 1
.所以程序打印出-9 3 0 1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.