[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arithmetic assignment side-effects

From: Chet Ramey
Subject: Re: Arithmetic assignment side-effects
Date: Tue, 06 Aug 2013 14:49:00 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130620 Thunderbird/17.0.7

On 8/5/13 10:57 PM, DJ Mills wrote:
> On Sun, Aug 4, 2013 at 4:41 PM, Andreas Schwab <address@hidden>wrote:
>> Chris Down <address@hidden> 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

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]