gpt4 book ai didi

c - 作业返回什么?

转载 作者:太空狗 更新时间:2023-10-29 16:21:18 28 4
gpt4 key购买 nike

为什么表达式 i = 2 返回 2?这是基于什么规则?

printf("%d\n", i = 2 );/* 打印 2 */

我在 Java/C# 中花费了很长时间后进入了 C 域。原谅我的无知。

最佳答案

计算2,因为标准就是这样定义它的。来自 C11 Standard , 第 6.5.16 节:

An assignment expression has the value of the left operand after the assignment

它允许这样的事情:

a = b = c;

(尽管对于这样的代码是否是一件好事存在一些争论。)

顺便说一下,这种行为在 Java 中得到了复制(我敢打赌它在 C# 中也是一样的)。

关于c - 作业返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9514569/

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