变量与&&和||结合使用

1 c boolean-operations

最后一行是什么意思?

a=0;
b=0;
c=0;

a && b++;
c || b--;
Run Code Online (Sandbox Code Playgroud)

你可以用更有趣的例子来解释这个问题吗?

Mar*_*off 10

对于你给出的例子:if a是非零的,增量b; 如果c为零,则递减b.

由于短路评估的规则,即.

您也可以使用函数作为右侧参数来测试它; printf这将是有益的,因为它给我们很容易观察到的输出.

#include <stdio.h>
int main()
{
    if (0 && printf("RHS of 0-and\n"))
    {
    }

    if (1 && printf("RHS of 1-and\n"))
    {
    }

    if (0 || printf("RHS of 0-or\n"))
    {
    }

    if (1 || printf("RHS of 1-or\n"))
    {
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

RHS of 1-and
RHS of 0-or
Run Code Online (Sandbox Code Playgroud)