northlane northlane - 6 months ago 29
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 get this error message.

test.c:3:5: error: lvalue required as increment operand

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.

§ (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". - § footnote 64).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download