operators... precedence and associativity

hi!
i am trying to run a use multiple operators in a single line... but i am finding it difficult to catch the flow of execution.

int x=3;
x-=--x-x--;

output is 1.

plz help me with "the compiler execution steps" of the instruction mentioned above...

thanks in advance...
Regards,
Arunkumar
Last edited on
hello.
this is what your code does, in order:
(x)(3) means the value of the expression x is 3
assigns 3 to x
(x)( 3 ) = ((x)(3) - ( (x-1)(2)-( x )(2) )(0))(3)
x = x - 1
x = x - 1
x == 1

so, to simplify,
x=3;
x-=0;
x--;
x--;
also, this is good as an example, but try not to use this in the future.
x-=--x-x--
The result of this expression is undefined.

On one compiler, it could be the same as
1
2
x=x-(x-1-x);
x=x-2;

While on a different compiler, it could be
1
2
3
4
temp0=x;
x=x-1;
x=temp0-(x-x);
x=x-1;


The order of evaluation is undefined, so it's never a good idea to reference a variable that is being decremented or incremented, more that once in the same expression.
Last edited on
Topic archived. No new replies allowed.