My question is maybe very simple, but I'm wondering what does this x+1 means? Let's see an example:
int main()
{
int x = 2;
x + 1; //1
if ((x - 2) && (x = 7)) { //2 and 3
// do something
}
}
What i know:
Questions:
I saw in debugger that this doesn't change the value of x, but I'm using a C++ compiler in Visual Studio so it can give another values.
Thanks in advance :)
The code
x+1;
evaluates the expression and then just drops the results. It's legal but a good compiler should issue a warning (IIRC g++ emits something about an expression that would require side effects to be useful).
The code (x - 2) && (x = 7)
instead doesn't do anything, because &&
is "short-circuited" and x-2
is false in a logical context. So the code (x = 7)
is not evaluated. &&
and ||
evaluate the left side first and the right side is evaluated only if the result cannot be determined from it... for example (1 || foo())
is guaranteed to skip the call to function foo
.
Code like
y = (x - 2) * (x = 7);
would instead be undefined behavior because C++ is not required to work through sub-expressions in sequence (except for the comma operator ,
, logical AND &&
, logical OR||
and the ternary operator ?:
) and using and modifying the same value in different parts of an expression (if these parts don't have a prescribed evaluation sequence) is not permitted but the compilers are not required to complain about it. Whatever happens happens, and it's a programmer's fault.
How does the memory looks like after operation 1?
It's the same as before the operation. x - 1
is an expression without side-effects (i.e. it doesn't modify any variable). The statement x - 1;
evaluates the expression and discards the result. Any decent compiler will optimize it away.
To increment x
by 1
you should use the compound assignment operator +=
:
x += 1;
The same can be achieved with the increment operator ++
:
x++; // these do the same
++x; // thing in this case
In other contexts, the two different versions of ++
have different meaning, see What is the difference between prefix and postfix operators?.
Is the value of x changed after x-2 (2)?
x - 2
itself doesn't change the value of x
. (Again x -= 2
can be used to achieve that.)
However the assignment x = 7
changes the value of x
to 7
, if the assignment is evaluated (which happens if the left-hand-side of &&
evaluates to true
or non-zero (this is called short-circuit evaluation). In this case the left-hand-side evaluates to zero so x = 7
is not evaluated).
Note that the =
operator is different to the equality comparison operator ==
:
x == 7 // evaluates to `true` if the value of `x` is equal to `7`, and to `false` otherwise.
It means "perform this calculation and throw away the result".
In your case that means the compiler will probably just remove the code completely (since it obviously has no side effects). But if operator+ had been overloaded and/or user-defined types had been involved, then there could be meaningful side-effects and the code could be meaningfull and would be kept to perform those operations..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With