Why cant i calculate how to get the sum?

The code compiles and it gives me -60 but i dont know how to get it.
i tried 6 + 6 - 6*6 and i got -24. Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
  #include<iostream>
using namespace std;

int main()
{
	int x = 6;

	x += x -= x*x;  	
cout << x << endl;  	
return 0;
}
Last edited on
you cant use x+ and -x on the same line bro.

Use 2 different variables.
The C++ language does not guarantee what the result of that line will be. It's simply undefined. You should avoid writing code like this that modifies and then reads the variable in the same expression.

The result is well-defined if you write it as two statements.
1
2
x -= x*x; // 6 - (6 * 6) = -30
x += x; // (-30) + (-30) = -60 
Thank you guys. Saved my life. I cant believe i couldnt figure it out
Topic archived. No new replies allowed.