C pre decrement problem

Int x=5;
Y= --x +( --x + --x); // outputs y as 6.
where as
Y= (–x + --x) + --x; outputs y as 8.
Any explanation? Please.

Both of the statements,

Y = --x + (–x + --x)


Y = (-x + --x) + --x

form invalid C code because they give rise to undefined behaviour (due to violation of the sequence point rules). No one really should write code like this.

But, if you are still interested, you should take a look at the assembly generated. Hope you can make sense out of that. :slight_smile:

1 Like

Hope This Help

Thanks for such fast response.

1 Like