c - x=x++;的作用是什么?

标签 c

这个问题在这里已经有了答案:





Why are these constructs using pre and post-increment undefined behavior?

(14 个回答)


8 年前关闭。



#include <stdio.h>

int main()
{
  int x=100;
  x=x++;
  printf("x : %d\n",x); //prints 101
  return 0;
}

输出101的原因是什么?我认为输出应该是100。

最佳答案

这是Undefined Behaviour , 由于 Sequence Points .

Between consecutive "sequence points" an object's value can be modified only once by an expression



上一期结束x=100;是一个序列点,结束x=x++;是另一个。

基本上,您的表达式没有中间的“序列点”,但您要修改 X 的值两次。这样做的结果是未定义行为:基本上,任何事情都可能发生:您可能会得到 100、101 或 42...

关于c - x=x++;的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16548238/

相关文章:

c - "Wide character array initialized from non-wide string"是什么意思,我如何在 C 中更正它?

c - C 预处理器插入的空格

c++ - GDB。只捕获未捕获的异常

c++ - GetCommandLine linux *true* 等效

c - 字符串中存在多个单词 C

c - 管道/ fork 有问题。说明书看不懂一半(man)

c - 根据文件大小动态分配数组大小 - C

c - 参数列表太长。构建 Hubbub HTML 解析库。执行

在堆栈中创建大内存

c - 如何在 LabWindows CVI 中读取网站上的文本?