northlane northlane - 1 month ago 9
C Question

C: Why can't you increment/decrement a variable twice in the same expression?

When I try to compile this code

int main() {
int i = 0;
++(++i);
}


I get this error message.


test.c:3:5: error: lvalue required as increment operand
++(++i);
^



What is the error message saying? Is this something that gets picked up by the parser, or is it only discovered during semantic analysis?

Answer Source

++i will give an rvalue1 after the evaluation and you can't apply ++ on an rvalue.

§6.5.3.1 (p1):

The operand of the prefix increment or decrement operator shall have atomic, qualified, or unqualified real or pointer type, and shall be a modifiable lvalue.


1. What is sometimes called "rvalue" is in this International Standard described as the "value of an expression". - §6.3.2.1 footnote 64).