3

Can someone please tell me why the C compiler outputs an error while using a Compound Assignment and a Prefix Dec/Inc together ? [ but C++ does not ]

int myVar = 5;
(--myVar) -= 4;
// C  : error C2106: '-=' : left operand must be l-value
// C++: myVar=0;

I know what the error says ...

But, I can't understand why a C compiler can't recognize myVar as a l-value but C++ does?!

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
Emadpres
  • 3,466
  • 2
  • 29
  • 44

1 Answers1

6

In C, prefix -- operator yields an rvalue. An rvalue can't be a left operand of assignment operator. That said, C and C++ are two different languages.

haccks
  • 104,019
  • 25
  • 176
  • 264