Confused with x++,++x and /

I get two codes.
1
2
3
4
5
6
double x=43;
double y=43;
double z;
x = x++/4;
y=++y/4;
cout << x << " " << y <<" "<< endl;

The output is 10.75 11.
At line 4, does the code mean that x=43/4 and then x is increased by 1?
but why x does not increased by 1?

1
2
3
4
5
6
double x=43;
double y=43;
double z;
z = x++/4;
y=++y/4;
cout << x << " " << y <<" "<< z << endl;

If I change the code like this, the output is 44 11 10.75,
x=44 and "++" operator works.
1
2
int x = 0;
y = x++;

I know that the code above will give a result of y=0 but I can't understand why the outputs of the first two codes are like that. Can anyone explain it to me. Thanks
Last edited on
At line 4, does the code mean that x=43/4 and then x is increased by 1?
This code is invalid. It has Undefined Behavior in it. That means anything can happen. It is completely plausible to have x equal to 0 after this line.
why x does not increased by 1?
Because it is undefined behavior and it happens to do that this time. I have 11.75 and get 10.75 when change optimisation options.

In second case there is no UB, so it works fine. Note that in both cases operations on y are undefined too and can cause problems anytime.

Any properly configured compiler should warn you about such simple cases:
\Test\main.cpp|16|warning: operation on 'x' may be undefined [-Wsequence-point]|
\Test\main.cpp|17|warning: operation on 'y' may be undefined [-Wsequence-point]|
I'm surprised it even works. As MiiNiPaa mentioned, what you're attempting to do has undefined behavior.

Given the fact that it does seem to work though, you just need to understand what procedures are done with the increment operators. ++x increments x and returns the incremented number, whereas x++ returns x and then increments it --> note that this is where the problem is occurring. The operation is never performed when you're performing multiple operations in a given expression.

Also, though it is not explicitly correct, ask yourself if this makes sense to you: x = (x = x + 1)/4. That is basically what you're trying to do with x++.

1
2
3
4
5
6
7
8
9
10
11
12
13
    double x = 43;
    
    cout << "Original X: " << x << endl;
    
    x = x++/4;
    
    cout << "First x = x++/4 X: " << x << endl; // returns 10.75

    x = x++/4;
    
    cout << "Second x = x++/4 X: " << x << endl; // returns 2.6875

    // 10.75 = 2.6875 * 4 


Interesting nonetheless.

EDIT: Just came across this in C++ Primer.


For operators that do not specify evaluation order, it is an error for an expression to refer to and change the same object. Expressions that do so have undefined behavior (§ 2.1.2, p. 36). As a simple example, the << operator makes no guarantees about when or how its operands are evaluated. As a result, the following output expression is undefined:

1
2
int i = 0;
cout << i << " " << ++i << endl; // undefined 


Because this program is undefined, we cannot draw any conclusions about how it might behave. The compiler might evaluate ++i before evaluating i, in which case the output will be 1 1. Or the compiler might evaluate i first, in which case the output will be 0 1. Or the compiler might do something else entirely. Because this expression has undefined behavior, the program is in error, regardless of what code the compiler generates.

There are four operators that do guarantee the order in which operands are evaluated. We saw in § 3.2.3 (p. 94) that the logical AND (&&) operator guarantees that its left-hand operand is evaluated first. Moreover, we are also guaranteed that the right-hand operand is evaluated only if the left-hand operand is true. The only other operators that guarantee the order in which operands are evaluated are the logical OR (||) operator (§ 4.3, p. 141), the conditional (? :) operator (§ 4.7, p. 151), and the comma (,) operator (§ 4.10, p. 157).
Last edited on
Topic archived. No new replies allowed.