[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ast-users] Arithmetic assignment side-effects
From: |
Roland Mainz |
Subject: |
Re: [ast-users] Arithmetic assignment side-effects |
Date: |
Sun, 4 Aug 2013 00:30:48 +0200 |
On Sun, Aug 4, 2013 at 12:04 AM, Dan Douglas <ormaaj@gmail.com> wrote:
> Is it specified what the value of x should be after this expression?
>
> x=0; : $((x+=x=1))
>
> Bash, ksh93, mksh, posh say 1. zsh, dash, busybox say 2. Clang and gcc both
> throw warnings about it, but both plus icc agree on 2.
Just curious: Is that x86-specific or is the result always the same on
other architectures, too ? Maybe there is something in ISO C1X/C99
which actually defines or recommends a specific compiler behaviour.
Maybe we should ask the "clang" people about the "why ?" ...
> <stdin>:1:42: warning: unsequenced modification and access to 'x'
> [-Wunsequenced]
> int main() { int x=0; printf("%d\n", x+=x=1); return 0; }
> ~~ ^
> 1 warning generated.
> 2
Mhhh... I tried this on SuSE 12.3/AMD64/64bit and Solaris 11 with Sun Studio 12:
-- snip --
$ cat -n x.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 int main(int ac, char *av[])
5 {
6 float x;
7
8 x=0;
9 ((x+=x=1));
10
11 printf("x=%f\n", x);
12
13 return(EXIT_SUCCESS);
14 }
15
$ cc -std=c99 -Wall x.c # or cc -xc99=%all x.c for Sun Studio
x.c: In function ‘main’:
x.c:9:5: warning: operation on ‘x’ may be undefined
$ ./a.out
x=2.000000
-- snip --
AFAIK the next best thing is to issue a warning for ksh -n that
((var1+=var1=othervaror_const)) creates undefined results (maybe we
get arithmetic optimisations in the future and then this will bite
back) ...
----
Bye,
Roland
--
__ . . __
(o.\ \/ /.o) roland.mainz@nrubsig.org
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)