On 8/5/13 10:57 PM, DJ Mills wrote: > On Sun, Aug 4, 2013 at 4:41 PM, Andreas Schwab <sch...@linux-m68k.org>wrote: > >> Chris Down <ch...@chrisdown.name> writes: >> >> x+=a is the same as x=x+a. Now replace a by (x=1) and it becomes >> obvious that 1 is a perfectly valid outcome. >> >> > I still don't see that; for one thing even with: > > #include <stdio.h> > > int main(void) { > int x = 0; > printf("%d\n", x = x + (x = 1)); > > return 0; > } > > I'm still seeing "assign x = 1, then do x + x, then assign the result to > x", which would give 2. gcc agrees with me. Especially with the parens > there, that's the order I would expect, but it's the same without as well.
It's undefined behavior. It's perfectly valid to evaluate the RHS of the assignment from left to right, since the parens don't change the evaluation order. Since it's undefined, it's better to stay away from the construct altogether. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/