Difference between -= and =- in C++ and C? [duplicate]
I often confuse both "-=" and "=-", like what is the exact difference between them?
int main()
{
int x=10, a=-3;
x=-a;
printf("%d",x);
return 0;
}
Output
3
Solution 1:
-=
is a compound assignment operator. =-
is two operators applied seperately.
While
a =- 3;
is the same as
a = (-3);
This
x -= a;
is more or less equivalent to
x = x - a;
"More or less" because operators can be overloaded (in C++) and typically the compound operator avoids the temporary right hand side.
Btw you are using =-
twice in your code while the questions asks for +=
vs =+
. +=
is a compound operator as well.