首页 > 解决方案 > 输出如何 1 和 4 。我无法理解c程序背后的逻辑?

问题描述

输出如何 1 和 4 。我无法理解c程序背后的逻辑?

#include <stdio.h>

int main()
{
    int a;
    int i = 4;
    a = 24 || --i;
    printf("%d %d",a,i);

    return 0;
}

输出

1 4

...Program finished with exit code 0
Press ENTER to exit the console.

谁能解释一下程序的逻辑…………

标签: c

解决方案


我认为语言 C 的值为 0 false,所有非零数字为true.

因此,从优化编译器的角度来看,您会得到:

a = 24 || --i;
a = TRUE OR --i;

=> "TRUE OR ..." is always TRUE, so just skip it!
Hence: 
a == 1 (which means "TRUE")

那么i呢?好吧,什么也没发生,所以它只是保持原来的价值。


推荐阅读