The following code prints -10
int x = 10;
-x;
cout << -x << endl; // printf("%d\n", -x);
both in C and C++ compilers (gcc 4.1.2). I was expecting a compiler error for the second line.
May be it is something fundamental, but I do not understand the behavior. Could someone please explain?
Thanks
Statements can be expressions. Such statements discard result of the expression, and evaluate the expression for its side effects.
-x;computes the negation ofxand discards the result.For more information read
[stmt.expr]in the C++ standard.