为什么输出中 c 不增加?

发布于 2025-01-08 16:47:39 字数 321 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我只土不豪 2025-01-15 16:47:40

变量 c 不会递增,因为除非 LHS 计算结果为 false,并且 LHS 计算结果为 true,否则不会执行 || 的 RHS(右侧)。 C ||&& 运算符是“短路”运算符;它们不会计算第二个操作数,除非第一个操作数不足以确定表达式的整体真实性。

&&|| 绑定更紧密,因此操作可以用括号括起来:

d = (++a && ++b) || ++c;

++a 的值为 -2 ,其计算结果为 true(因为任何非 0 的值都计算为 true); ++b 的值为 3,其计算结果为 true;因此 && 项的值为 true。因为 true || falsetrue || 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:

d = (++a && ++b) || ++c;

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. Because true || false and true || true both evaluate to true, 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 had a = -1; before the test, then b would not be incremented, because ++a would be zero or false, so the RHS of the && is unevaluated. Of course, then c would be incremented because the LHS of the || would be false, and the RHS would have to be evaluated to determine the overall result.)

穿透光 2025-01-15 16:47:40

因为 ++a && ++b 计算结果为 true

这就是所谓的短路。条件内的表达式从左到右进行计算。如果您的情况,如果 OR 子句中的第一个条件被评估为 true,则没有必要评估第二个条件,因为已知整个表达式已经是true了。

Because ++a && ++b evaluates to true.

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 to true, there's no point for the second one to also be evaluated, since the whole expression is known to be true already.

盛夏已如深秋| 2025-01-15 16:47:40

在 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.

旧街凉风 2025-01-15 16:47:40

它是惰性布尔表达式求值。
执行是:

++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 gives 3

-2 && 3 gives 1

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). Here 1 is "not 0" of course.

无远思近则忧 2025-01-15 16:47:40
d= ++a && ++b || ++c
d= ++(-2) && ++b || ++c
d= -1 && ++b || ++c
d= true && ++b || ++c
d= true && ++2 || ++c
d= true && true || ++c
d= true || ++c
d= 1

这大概就是它在幕后的运作方式......

d= ++a && ++b || ++c
d= ++(-2) && ++b || ++c
d= -1 && ++b || ++c
d= true && ++b || ++c
d= true && ++2 || ++c
d= true && true || ++c
d= true || ++c
d= 1

That's roughly how it works behind the scene...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文