gpt4 book ai didi

C编程条件运算符

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:24 26 4
gpt4 key购买 nike

如此简单,但有一点我无法理解。

    #include <stdio.h>
#include <stdlib.h>

int main () {

int n=15, p=10, q=5;
q=n<p? n++: p++;
printf ("n=%d p=%d q=%d \n" ,n,p,q);

return 0;
}

在使用条件运算符时,它会检查 if(5=15<10) n++ else q++

答案:n=15 p=11 因为这个陈述是错误的,而且 q=10 而不是 15。这是为什么?所以 q=p 而不是 q=n。

谁能给我解释一下?

最佳答案

it checks if(5=15<10) n++ else q++

不正确。

q=n<p? n++: p++;

是一个作业

6.5.16.1 部分的 C 稿p2 说:

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

在你的例子中,只是右操作数的值是从表达式 n<p? n++: p++ 中推导出来的。 .

还有两个前缀表达式 n++p++ n 的值和 p在根据分支考虑增量之前。

关于C编程条件运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47498747/

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