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

标签 c c99 conditional-operator

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

(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/

相关文章:

c - 在以下 lex/yacc 文件中接收段错误(核心转储)。他们怎么了?

c - 访问无符号整数的结构数组

用于方法调用的 Javascript 三元运算符

c - 停止等待协议(protocol) - 它如何处理最后一个 ACK​​ 丢弃?

c - MPI C如何访问其他级别的结构/变量

c - 写入 mmap 文件时出现总线错误

c - 分配给作为结构成员的指针会导致段错误

C99 单行比较语法快捷方式

awk - 如果 awk 中未设置变量,则设置变量

c - 这段代码的作用是什么以及使用的每个符号的含义是什么?使用的语言是C,我只是编码的初学者