2012-11-29 2 views
2

가능한 중복 : 그것을 실행할 때
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)일부 C 출력을 설명 할 수 있습니까?

#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 나에게 출력을주고있다. 누군가 어떻게 이런 일이 발생했는지 정확히 설명 할 수 있습니까? 당신이 sequence points 사이에 두 번 a을 변경하고 이후

답변

3

코드의 동작은 undefined입니다 :

call(a,a++,++a); 
3

behaviour is undefined 두 시퀀스 지점 사이에 두 번 변수를 변경하기 때문이다.

"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 두 개의 서열 사이의 지점을 수정하는

2

c99 standard : 5.1.2.3 Program execution 
.

확장 편집 : 이미 그 , 콤마 연산자에 대해 생각이 개념을 알고 있다면 당신은 함수 호출에 여기에 사용 ,을 잘못 program.Then 그것은 well defined으로 작동해야하도록 시퀀스 지점입니다 comma separator하지 comma operator

입니다
관련 문제