Why are multiple increments / decrements valid in C ++, but not in C?

test. (C / CPP)

#include <stdio.h> int main(int argc, char** argv) { int a = 0, b = 0; printf("a = %d, b = %d\n", a, b); b = (++a)--; printf("a = %d, b = %d\n", a, b); return 0; } 

If I save the above as a .cpp file, it compiles and displays this on execution:

 a = 0, b = 0 a = 0, b = 1 

However, if I save it as a .c file, I get the following error:

 test.c:7:12: error: lvalue required as decrement operator. 

Should the operation (++a) be allowed before the operation (newValue)-- ? Does anyone know about this?

+6
source share
4 answers

In C, the result of the prefix and postfix increment / decment operators is not the value of l.

In C ++, the result of a phased increment / decment statement is also not an lvalue value, but the result of a prefix increment / decrement statement is an lvalue.

Now doing something like (++a)-- in C ++ - this is undefined behavior, because you change the value of the object between two points in the sequence twice.

EDIT: after the comment by @ bames53. This behavior is undefined in C ++ 98 / C ++ 03, but changes in C ++ 11 based on the idea of ​​sequence points now define this expression.

+13
source

In C and C ++, there are lvalue expressions that can be used on the left side of the = operator and rvalue expressions that may not match. C ++ allows more things to be lvalues ​​because it supports referential semantics.

 ++ a = 3; /* makes sense in C++ but not in C. */ 

The increment and decrement operators are similar to assignments, since they change their argument.

In C ++ 03 (++a)-- will lead to undefined behavior, because two operations that are not ordered relative to each other change the same variable. (Despite the fact that one of them is "pre" and one is "post", they are not affected by the consequences, because there is no && ? Or such.)

In C ++ 11, an expression now does what you expect. But C11 does not change any such rules, this is a syntax error.

+3
source

For those who want accurate information on the differences, as indicated in the standards, C99, Β§6.5.3 / 2 reads:

The operand value of the prefix ++ operator is incremented. The result is a new operand value after the increment.

Unlike C ++ 11, Β§5.3.2 / 1 says:

The result is an updated operand; it is an lvalue , and it is a bit field, if the operand is a bit field.

[highlight added in both cases]

Also note that although (++a)-- gives undefined behavior (at least in C ++ 03) when a is int , if a is a specific user type, so you use your own ++ overloads and -- , the behavior will be determined - in this case you get the equivalent:

 a.operator++().operator--(0); 

Since each operator leads to a function call (which cannot intersect), you actually have sequence points to force a specific behavior (note that I do not recommend using it, only noting that the behavior is really defined in this case).

+2
source

Β§5.2.7 Increment and decrement:

The value of the post fi x ++ expression is the value of its operand. [...] The operand must be a modified lvalue .

The error you get in your C compilation suggests that this is only a function present in C ++.

0
source

All Articles