0

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
Tabz
  • 3
  • 3
  • @pmg I didn't get the token part? can you clarify please? – Tabz Jan 13 '22 at 11:35
  • In the process of compilation, one of the earlier tasks is dividing the text of the source file into tokens. The tokens are then used further along to create the object file. For example, `---` is transformed into two tokens `--` and `-`; `-++` is transformed into the two tokens `-` and `++` – pmg Jan 13 '22 at 11:42

1 Answers1

4

-= 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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185