关于逻辑运算符的基本编程练习

Basic programming exercise about logical operators

我的书中有一个问题:

#include<stdio.h>
void main()
{
    int a=5, b=-7, c=0, d;
    d = ++a && ++b || ++c;
    printf("\n%d%d%d%d",a,b,c,d);
}

问题问我代码的输出是什么。我 运行 它和屏幕上的结果是 6-601。我明白为什么a=6b=-6,但我不明白为什么c=0d=1

|| OR 运算符是短路的,这意味着如果左侧为真,则不评估右侧。在这种情况下 ++a && ++b 的计算结果为真,因此 ++c 永远不会 运行 并且 c 保持其值为零。

此外,由于它的计算结果为真,因此用存储在 d 中的 1 表示。

任何非零值都被认为是真实的,布尔运算的结果被定义为 01 作为整数。

我相信你已经得到了答案,但为了一步一步地详细说明,让我在这里再补充一点。首先,引用 &&|| 运算符的属性,分别来自 C11 标准,章节 §6.5.13 和 §6.5.13,

(我)

The && operator shall yield 1 if both of its operands compare unequal to 0; otherwise, it yields 0. [...] If the first operand compares equal to 0, the second operand is not evaluated.

(二)

The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. [...]. If the first operand compares unequal to 0, the second operand is not evaluated.

并且它们都保证从左到右的评估。所以,比较你的代码,

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

它就像

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

计算结果为

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

然后

d = ( ( 6 && (-6) ) || ++c); 

现在在上面的阶段,(I)已经实现,归结为

d = ( 1 || ++c);

现在,随着重点已经满足(II),所以不再对||的RHS操作数进行进一步的评估(即不评估++c),并且它似乎是 d = 1 并且最终结果 1 存储到 d.

就是这样,a == 6b == -6c == 0d ==1


话虽如此,void main()应该改为int main(void),至少要符合标准。