Assignment of variables and use in one expression

I came across a line of code:

int a = 10;
int b = 40;
a = a + b - (b = a);
cout << a << "   " << b << endl;

I can not understand what is happening in this code. Can someone explain to me?

+5
source share
3 answers

Undefined behavior . the valuebchanges and used to calculate without an intermediate point in the sequence. The results of the program are unpredictable - it may print something or crash, or make some unpleasant system calls.

, , , , .53) , . , , . ; undefined.

+11

This behavior is undefined because vairable b was modified and then used in a single expression, so the final result is ambiguous because it depends on the order of the evalution expression. (b=a)may occur before or after b vairable used for calcualte a+b.

0
source

All Articles