gpt4 book ai didi

c - 解释一下逻辑

转载 作者:行者123 更新时间:2023-11-30 21:46:09 25 4
gpt4 key购买 nike

  #include <stdio.h>
//Compiler version gcc 6.3.0

int main(void)
{
int i=10, j=2, k=0, m;
m=++i || ++j && ++k;
printf("%d,%d,%d,%d",i,j,k,m);
}

can anyone explain the logic, Output is 11,2,0,1

最佳答案

由于操作的优先级,该表达式语句

m=++i || ++j && ++k;

相当于

m = ++i || ( ++j && ++k );

你可以想象它就像

m = expression1 || expression2;

其中表达式1++i表达式2(++j &&++k)

根据C标准(6.5.14逻辑OR运算符)

4 Unlike the bitwise | operator, the || operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares unequal to 0, the second operand is not evaluated.

由于表达式1 与 0 比较不等于(其值等于 11),因此不会计算表达式2

因此仅计算表达式1,并且它不等于0。

根据 C 标准同一部分的另一段引用

3 The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

右侧的计算结果等于 1,并赋值给变量 m

因此只有变量im被改变。变量 jk 没有更改,因为它们作为操作数出现的表达式没有被求值。

关于c - 解释一下逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46606943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com