2

可能重复:
谁能解释这些未定义的行为(i = i++ + ++i,i = i++ 等……)

#include<stdio.h>
void call(int,int,int);

int main(){

int a=10;
call(a,a++,++a);
return 0;   
}

void call(int x,int y,int z){
printf("x=%d y=%d z=%d\n",x,y,z);
}

这段代码在运行时给了我 12 11 12 的输出。有人可以准确解释这是怎么发生的吗?

4

2 回答 2

3

您的代码行为未定义,因为您在序列点a之间更改了两次:

call(a,a++,++a);
于 2012-11-29T15:20:15.247 回答
3

因为behaviour is undefined在两个序列点之间两次更改变量。

c99 standard : 5.1.2.3 Program execution

2

"Accessing a volatile object, modifying an object, modifying a file, or calling a function
that does any of those operations are all `side effects` which are changes in the state of
the `execution environment`. Evaluation of an expression may produce side effects. At
certain specified points in the execution sequence called `sequence points`, all side effects
of previous evaluations shall be complete and no side effects of subsequent evaluations
shall have taken place."

在这里,您a在两个序列点之间修改了两次变量。

扩展编辑:如果您已经知道这些概念并且考虑到,逗号运算符是一个序列点,所以它应该作为一个程序工作。那么您在函数调用中的使用well defined是错误的,comma separatorcomma operator

于 2012-11-29T15:22:51.830 回答