I have some C code:
main()
{
int a=1;
void xyz(int,int);
xyz(++a,a++); //which Unary Operator is executed first, ++a or a++?
printf("%d",a);
}
void xyz(int x,int y)
{
printf("\n%d %d",x,y);
}
The function xyz
has two parameters passed in, ++a
and a++
. Can someone explain the sequence of operations to explain the result?
The above code prints "3 13" or "2 23" depending on which compiler is used.
Well, there are two things to consider with your example code:
++a
or a++
is evaluated first is implementation-dependent.a
more than once without a sequence point in between the modifications results in undefined behavior. So, the results of your code are undefined.If we simplify your code and remove the unspecified and undefined behavior, then we can answer the question:
void xyz(int x) { }
int a = 1;
xyz(a++); // 1 is passed to xyz, then a is incremented to be 2
int a = 1;
xyz(++a); // a is incremented to be 2, then that 2 is passed to xyz