gpt4 book ai didi

c - 赋值运算符和序列点的副作用

转载 作者:行者123 更新时间:2023-12-05 01:35:05 28 4
gpt4 key购买 nike

我正在为强调的行寻找一些说明。

(C99 6.5.16/3) An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand. The side effect of updating the stored value of the left operand shall occur between the previous and the next sequence point.

考虑以下示例:

int x = 42;
int y = 0;

int main()
{
// side effect of modifying 'x'
x = y;
}

什么是上一个和下一个序列点? main的开头有顺序点吗?

最佳答案

C99 5.1.2.3 将序列点定义为先前评估的所有副作用已经发生并且后续评估的副作用尚未开始发生的地方。标准的附件 C 定义了序列点发生的位置:函数调用、逻辑运算符的结尾、逗号运算符和三元运算符、完整声明的结尾、完整表达式的结尾等等。

在这种情况下,前一个序列点是main()的开始,下一个序列点是赋值结束的分号。在第一个序列点,x 的值为 42,在第二个点,它将为 0。

关于c - 赋值运算符和序列点的副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14069516/

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