Problem in uderstanding associativity and a strange situation

nishu's Avatar
Go4Expert Member
//Problem in uderstanding associativity and in the following two situations:

1. If I write in my program

int a=10;
c=++a - a++ - ++a - a++;
printf("%d",c); //output=-24

2.If I write
printf("%d",++a - a++ - ++a - a++); //output=-26

Why does it happen?

In situation 1 value is evaluated as follows:

c=++a - a++ - ++a - a++
c=11-a++ -++a-a++
c=11-11 - ++a - a++

We are evaluating it from left to right.
But Associtivity of Increment(++) and decrement (--) operator is right to left.Don't you think we should evaluate it from right to left? If not please tell me why.

I will be very thankful to you for giving its answer.
micsom's Avatar, Join Date: Oct 2008
Go4Expert Member
i think you are using turboC....i too observed this problem in my first yr in college..
but try using other compilers(which are more recent)...
there you will get the same -24

i dont exactly know the reason for this problem,but i found it a problem which occured with the way a function is called.
xpi0t0s's Avatar, Join Date: Aug 2004
Associativity has nothing to do with your confusion. Associativity rules don't apply to pre- and post- increment because they take just a single variable. Addition is associative because a+(b+c) = (a+b)+c. Subtraction is not associative: (1-2)-3=-1-3=-4. 1-(2-3)=1-(-1)=0.

How a compiler handles pre- and post-increment is not defined by the standard and in particular how multiple side effects operating on the same variable in the same expression is left to the compiler writers to determine how it works. So the result depends on the compiler and is very non-portable. Visual Studio handles pre- side effects before evaluating an expression and post- side effects afterwards, evaluating the expression with fixed values. So c=++a - a++ - ++a - a++ is handled as follows: a++; a++; c=a-a-a-a; a++; a++; and since a starts out as 10 c=12-12-12-12=-24. TurboC could well be different. Visual Studio generates the following assembly (my comments prefixed with ---):
	int c=++a - a++ - ++a - a++;
00413655  mov         eax,dword ptr [a] 
00413658  add         eax,1 
0041365B  mov         dword ptr [a],eax  --- a preincrement
0041365E  mov         ecx,dword ptr [a] 
00413661  add         ecx,1 
00413664  mov         dword ptr [a],ecx --- another preincrement
00413667  mov         edx,dword ptr [a] 
0041366A  sub         edx,dword ptr [a] 
0041366D  sub         edx,dword ptr [a] 
00413670  sub         edx,dword ptr [a] 
00413673  mov         dword ptr [c],edx  --- the expression (c=a-a-a-a)
00413676  mov         eax,dword ptr [a] 
00413679  add         eax,1 
0041367C  mov         dword ptr [a],eax --- a post-increment
0041367F  mov         ecx,dword ptr [a] 
00413682  add         ecx,1 
00413685  mov         dword ptr [a],ecx --- another post-increment
> In situation 1 value is evaluated as follows

Are you sure about that? How did you determine how it was evaluated? The only way to be certain is to see what assembly code is generated by the compiler. In any case, clearly you're wrong, because you get -26 but the compiler gets -24.
nishu's Avatar
Go4Expert Member
Thank you for your reply.