Search code examples
c++crvaluelvalue

Why pre-increment operator gives rvalue in C?


In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why?


Solution

  • C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented).

    C99 6.5.3.1/2

    The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++Eis equivalent to (E+=1).

    ‘‘value of an expression’’ <=> rvalue

    However for historical reasons I think "references not being part of C" could be a possible reason.