当评估顺序指定为“从左到右”并且语言是(伪)类 C时,以下示例中的序列点是什么?
int x = 1;
int z = x-- + x; // z = 1 + 0 or z = 1 + 1?
my_func(x++); // x incremented before or after my_func execution?
my_func(x++ + --x); // combining those above
当评估顺序指定为“从左到右”并且语言是(伪)类 C时,以下示例中的序列点是什么?
int x = 1;
int z = x-- + x; // z = 1 + 0 or z = 1 + 1?
my_func(x++); // x incremented before or after my_func execution?
my_func(x++ + --x); // combining those above
序列点是语言标准定义的序列点。我即将给出的答案适用于 C,但另一种“类 C”语言可能很好地定义了不同的序列点,因此对这些问题有不同的答案。
int z = x-- + x; // z = 1 + 0 or z = 1 + 1?
由于+
不是 C 中的序列点,因此上述语句的结果是未定义的。
my_func(x++); // x incremented before or after my_func execution?
x
在运行之前递增my_func
,但my_func
使用旧值x
作为参数调用。
my_func(x++ + --x); // combining those above
未定义的原因与第一个相同。