简体   繁体   English

这个逻辑表达式是如何计算的

[英]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:以下是主要概念:

  1. Pre- and post- increment and decrement.前后递增和递减。 ++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
  2. Operator precedence . 运算符优先级 Specifically, && 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++);
  3. Short-circuit boolean evaluation .短路 boolean 评估 This says that, for operators && 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. ,如果左项的值足以确定表达式的结果,则不计算右项。
  4. Expression evaluation order .表达式求值顺序 The link shows a whole list of evaluation rules for C++ (I'm assuming C++11).该链接显示了 C++ 的评估规则的完整列表(我假设 C++11)。 In particular, item 6 says (paraphrasing) that the left-hand arguments of ||特别是,第 6 项说(释义) ||的左侧 arguments and && are always evaluated before right-hand arguments, provided those operators are not overridden.&&总是在右手 arguments 之前评估,前提是这些运算符没有被覆盖。
  5. Implicit casting to boolean.隐式转换为 boolean。 In C++, if an 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:把这一切放在一起,这就是发生的事情:

  • After the first line, a is 10, b is 3, c is 0 and d is unset.在第一行之后,a 为 10,b 为 3,c 为 0,d 未设置。
  • Next, the variable 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。
  • When cast to a boolean, the value 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++ ,因此不会发生增量。
  • The value to be assigned to d is true , but cast to an int .要分配给d的值是true ,但要转换为int The result is 1, so we have d = 1 .结果是 1,所以我们有d = 1
  • Finally, the values are: a = -9, b = 3, c = 0, d = 1.最后,值为:a = -9,b = 3,c = 0,d = 1。

So the program prints out -9 3 0 1 .所以程序打印出-9 3 0 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM