I need a and b value.
a=2;
b=a+=++a;
a+=-b;
Solution is:
a=0
b=6
Why is this the right answer? I know it's simple but I am sure I'm doing something wrong. Tried so many times to do it that I can't even think.
I need a and b value.
a=2;
b=a+=++a;
a+=-b;
Solution is:
a=0
b=6
Why is this the right answer? I know it's simple but I am sure I'm doing something wrong. Tried so many times to do it that I can't even think.
b=a+=++a; invokes undefined behavior. The code is simply wrong and buggy, since the different side effects on a aren't sequenced in relation to each other. There is no expected outcome and there is no point in reasoning about different results you might be getting.
Details can be found at the extremely common FAQ over at SO: Why are these constructs using pre and post-increment undefined behavior?