2

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

#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);
}

This code is giving me output of 12 11 12 when run it. Could someone explain exactly how this is happening?

Community
  • 1
  • 1
Roy
  • 316
  • 4
  • 11

2 Answers2

3

The behaviour of your code is undefined since you're changing a twice between sequence points:

call(a,a++,++a);
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

The behaviour is undefined because changing a variable twice between two sequence points.

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."

Here you are modifying a variable a twice between two sequence points.

Extended EDIT : If you know these concept already and thinking about that , comma operator is a sequence point so it should work as a well definedprogram.Then you are wrong , used here in function call is comma separator not comma operator

Omkant
  • 9,018
  • 8
  • 39
  • 59