Search code examples
c++ccompiler-errorscompound-assignmentprefix-operator

C: Error in Using: "Compound Assignment" and "Prefix Decrement" together


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?!


Solution

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