为什么输出中 c 不增加?
我正在学习 C 基础知识,并试图解决以下问题,有人可以解释为什么变量 c
的输出不同吗?
以下程序的输出是什么?
int main()
{
int a = -3, b = 2, c= 0, d;
d = ++a && ++b || ++c;
printf ("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
}
Ans: -2, 3, 0, 1
为什么输出中 c
没有递增?
I was working on the Basics of C and was trying to solve the problem below could any one explain why the output of variable c
is different?
What is the output of the following program?
int main()
{
int a = -3, b = 2, c= 0, d;
d = ++a && ++b || ++c;
printf ("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
}
Ans: -2, 3, 0, 1
Why c
is not incremented in the output ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
变量
c
不会递增,因为除非 LHS 计算结果为 false,并且 LHS 计算结果为 true,否则不会执行||
的 RHS(右侧)。 C||
和&&
运算符是“短路”运算符;它们不会计算第二个操作数,除非第一个操作数不足以确定表达式的整体真实性。&&
比||
绑定更紧密,因此操作可以用括号括起来:++a
的值为 -2 ,其计算结果为 true(因为任何非 0 的值都计算为 true);++b
的值为 3,其计算结果为 true;因此&&
项的值为 true。因为true || false
和true || true
均评估为true
,无需评估 RHS 即可了解总体结果。 (&&
的类似规则是,如果第一项计算结果为 false,则无需计算第二项,因为整个表达式必须为 false。如果您有a = -1;
在测试之前,则b
不会递增,因为++a
会为零或 false,因此的 RHS &&
未被评估。当然,那么c
将会递增,因为||
的 LHS 将为 false,并且必须评估 RHS 才能确定总体结果。)The variable
c
is not incremented because the RHS (right-hand side) of an||
is not executed unless the LHS evaluates to false, and the LHS evaluates to true. The C||
and&&
operators are 'short-circuit' operators; they do not evaluate the second operand unless the first is insufficient to determine the overall truth of the expression.The
&&
binds tighter than the||
, so the operation can be parenthesized as:The value of
++a
is -2, which evaluates to true (because any value that is not 0 evaluates to true); the value of++b
is 3, which evaluates to true; so the value of the&&
term is true. Becausetrue || false
andtrue || true
both evaluate totrue
, there is no need to evaluate the RHS to know the overall result. (The analogous rule for&&
is that if the first term evaluates to false, there is no need to evaluate the second because the overall expression must be false. If you hada = -1;
before the test, thenb
would not be incremented, because++a
would be zero or false, so the RHS of the&&
is unevaluated. Of course, thenc
would be incremented because the LHS of the||
would be false, and the RHS would have to be evaluated to determine the overall result.)因为
++a && ++b
计算结果为true
。这就是所谓的短路。条件内的表达式从左到右进行计算。如果您的情况,如果
OR
子句中的第一个条件被评估为true
,则没有必要评估第二个条件,因为已知整个表达式已经是true
了。Because
++a && ++b
evaluates totrue
.It's called short-circuiting. Expressions inside conditions are evaluated from left-to-right. If your case, if the first condition in the
OR
clause is evaluated totrue
, there's no point for the second one to also be evaluated, since the whole expression is known to betrue
already.在 C 语言中,布尔逻辑运算符
&&
和||
是 短路。这意味着如果评估左侧不足以知道答案,他们只会评估右侧。对于您的代码,这会导致永远不会计算
++c
,因为左侧不为零,因此布尔 or 的结果将为 true,并且不需要做更多工作。In C, the boolean logic operators
&&
and||
are short-circuiting. This means that they only evaluate their right side, if evaluating the left side is not enough to know the answer.For your code, this has the effect of never evaluating
++c
, since the left-hand side is not zero and thus the boolean or's result will be true, and there's no need to do more work.它是惰性布尔表达式求值。
执行是:
++a
给出-2
++b
给出3
-2 & & 3
给出1
好的!无需检查
||
的结果。所以++c
不会被评估。规则是:在以下情况下不计算表达式部分
X
:(0 && X)
、(1 || X)
。这里的1
当然是“非0”。It is lazy boolean expressions evaluation.
The execution is:
++a
gives-2
++b
gives3
-2 && 3
gives1
Okay! No need to check result of
||
. So++c
is not evaluated.The rule is: expression part
X
is not evaluated in cases:(0 && X)
,(1 || X)
. Here1
is "not 0" of course.这大概就是它在幕后的运作方式......
That's roughly how it works behind the scene...