#include <stdio.h>
int main(void)
{
int i = 0;
i = i++ + ++i;
printf("%d\n", i); // 3
i = 1;
i = (i++);
printf("%d\n", i); // 2 Should be 1, no ?
volatile int u = 0;
u = u++ + ++u;
printf("%d\n", u); // 1
u = 1;
u = (u++);
printf("%d\n", u); // 2 Should also be one, no ?
register int v = 0;
v = v++ + ++v;
printf("%d\n", v); // 3 (Should be the …Run Code Online (Sandbox Code Playgroud) c increment operator-precedence undefined-behavior sequence-points
C和C++中未定义,未指定和实现定义的行为有什么区别?
c c++ undefined-behavior unspecified-behavior implementation-defined-behavior
我知道诸如x = x++ + ++x调用未定义行为之类的事情,因为变量在同一序列点内被多次修改。这在这篇文章中进行了彻底的解释为什么这些构造使用前增量和后增量未定义行为?
但是考虑像printf("foo") + printf("bar"). 该函数printf返回一个int,因此该表达式在这个意义上是有效的。但是+标准中没有规定运算符的求值顺序,所以不清楚这会打印foobar还是barfoo。
但我的问题是这是否也是未定义的行为。