I’ve got the following code:
#include <stdio.h>
int main(int argc, char **argv) {
int i = 0;
(i+=10)+=10;
printf("i = %d\n", i);
return 0;
}
If I try to compile it as a C source using gcc I get an error:
error: lvalue required as left operand of assignment
But if I compile it as a C++ source using g++ I get no error and when i run the executable:
i = 20
Why the different behavior?
Semantics of the compound assignment operators is different in C and C++:
C99 standard, 6.5.16, part 3:
In C++ 5.17.1:
EDIT : The behavior of
(i+=10)+=10in C++ is undefined in C++98, but well defined in C++11. See this answer to the question by NPE for the relevant portions of the standards.