5

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:

  • That the assignment cannot be evaluated because left side of && will return false, so the conjunction will never be true.

Questions:

  • How does the memory looks like after operation 1?
  • Is the value of x changed after x-2 (2)?

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 :)

tdbr
  • 316
  • 1
  • 3
  • 9

4 Answers4

4

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.
Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
4

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.

6502
  • 112,025
  • 15
  • 165
  • 265
  • umm.. are u sure? A conjuction i think checks both sides of && because the statement is true if both are true or false in any other combination – tdbr Jan 30 '17 at 17:46
  • 2
    @tdbr: No... `&&` and `||` are special... they evaluate the left side and the right side is evaluated **only if the result cannot be determined from the left side result**. – 6502 Jan 30 '17 at 17:47
3

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..

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

x + 1 does not change value of x. The result of x + 1 viz 3 is calculated and the result is then ignored.

For and (&&) sequence of evaluation is left to right. As long as the components of && operator are true, the next component is evaluated.

As stated earlier for x + 1, similarly, x + 2 does not change value of x. In your case, x - 2 results into 0 viz. zero and so the next component is not evaluated.

The basic principles of C language, remain same across all compilers and IDE (in your case Visual Studio) has no effect on the compilation

Ashish kulkarni
  • 644
  • 2
  • 7
  • 13