in GCC and VS2008
the fllow code
int a = 0, b;
b = a = a++;
printf("%d, %d\n", a, b);
result is : 1, 0
in Java
they are all 0.
In this context, your:
a++
expression is similar to the expression:
((a = a + 1), (a - 1))
except that there is no sequence point in 'a++' and there is only one
read access in 'a++' and one write access in 'a++'.
Behold the following events for the following expressions:
a++
A.1. Read the value of 'a' before the next sequence point
A.2. Increment the value of 'a' (a write) before the next sequence point
a = ...
B.1. Evaluate the expression '...' before the next sequence point
B.2. Store that value to 'a' before the next sequence point
b = ...
C.1. Evaluate the expression '...' before the next sequence point
C.2. Store that value to 'b' before the next sequence point
Note the ordering.
So we get the following order for your special line:
b = a = a++;
1. Evaluate the expression 'a = a++' (C.1)
2. Evaluate the expression 'a++' (B.1)
3. Read the value of 'a' (A.1)
?. Any time now or before the next sequence point, increment the value
of 'a' (A.2)
?. Any time now or before the next sequence point, store the value to
'a' (B.2)
4. There's no time left before the next sequence point, so we must store
the value to 'b' (C.2)
Note how #4 is ordered relative to #1, #2, #3, but unordered relative to
the two '?' operations. The '?' operations and #4 must occur after #3,
but can occur in any order.
Since there are two "write" operations to 'a' that can potentially occur
at the same time, that results in undefined behaviour.
Maybe the increment of 'a' happens first. Maybe the other store to 'a'
happens first. Maybe they happen at exactly the same time and the
computer crashes. Other "maybes" are possible too, since there are no
defined limits to what can happen.