bug-bash
[Top][All Lists]
Advanced

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

Re: Arithmetic assignment side-effects


From: DJ Mills
Subject: Re: Arithmetic assignment side-effects
Date: Tue, 6 Aug 2013 05:31:42 -0400

On Tue, Aug 6, 2013 at 3:20 AM, Andreas Schwab <schwab@linux-m68k.org>wrote:

> DJ Mills <danielmills1@gmail.com> writes:
>
> > I still don't see that;
>
> Go strictly from left to right.
>
>
 It doesn't make any kind of logical sense to do so. Yes, then the "(x =
1)" would be evaluated
last, but how do you evaluate the entire statement, for the first "x = "?
You can say "go
left to right", but if you think about it. that's not really possible.

Unless you can explain it to me...


> gcc agrees with me.
>
> No, it doesn't.
> $ cat cascade.c
> #include <stdio.h>
>
> int main(void) {
>   int x;
>
>   x = 0;
>   printf("\nx += x = 1\n%d\n", x += x = 1);
>
>   x = 0;
>   printf("\nx = x + (x = 1)\n%d\n", x = x + (x = 1));
>
>   return 0;
> }
>
> $ gcc -Wall -o cascade cascade.c
> cascade.c: In function 'main':
> cascade.c:7:36: warning: operation on 'x' may be undefined
> [-Wsequence-point]
> cascade.c:10:37: warning: operation on 'x' may be undefined
> [-Wsequence-point]
>
> $ ./cascade
> x += x = 1
> 2
>
> x = x + (x = 1)
> 2
>
> $ gcc -version | head -n 1
> gcc (GCC) 4.7.2
>
>
>
>>
>> Parens have no effect at all.
>>
>> It won't even compile without them, I get:
> cascade.c:13:43: error: lvalue required as left operand of assignment


Line 13 in that case being:
  printf("\nx = x + x = 1\n%d\n", x = x + x = 1);

That makes sense to me (getting an error thrown, I mean) .


reply via email to

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