virgil_van_maguire's blog

By virgil_van_maguire, history, 34 hours ago, In English

guess the value of each of the following and explain your logic. (please don't run it, have fun guessing)

    int x=5;
    cout<< x++ + ++x<<endl;
    x=5;
    cout<< ++x + x++<<endl;
    x=5;
    cout<<++x + ++x<<endl;
    x=5;
    cout<<++x + ++x + ++x<<endl;
  • Vote: I like it
  • +12
  • Vote: I do not like it

»
34 hours ago, # |
Rev. 6   Vote: I like it +4 Vote: I do not like it

x++ + ++x=12

++x + x++=12

++x + ++x=13

++x + ++x + ++x=21

  • »
    »
    34 hours ago, # ^ |
      Vote: I like it +17 Vote: I do not like it

    thanks for Rev. 2 without it i would've never understood.

  • »
    »
    34 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    How did you evaluate them to these values? can you please explain?

»
34 hours ago, # |
  Vote: I like it +2 Vote: I do not like it
first one:
second one:
third one:
fourth:
»
34 hours ago, # |
  Vote: I like it +52 Vote: I do not like it

caw caw caw

»
33 hours ago, # |
Rev. 2   Vote: I like it +40 Vote: I do not like it

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/n4950.pdf

6.9.1.10 If a side effect on a memory location (6.7.1) is unsequenced relative to either another side effect on the same memory location or a value computation using the value of any object in the same memory location, and they are not potentially concurrent (6.9.2), the behavior is undefined.

Meaning: x++ changes memory state under x, so does x++ and CPU instructions can be arranged in any way because standard not require which increment goes first.

Every cout there is UB.

»
33 hours ago, # |
Rev. 2   Vote: I like it +34 Vote: I do not like it

All of these are undefined behaviour, because the increment operations are unsequenced with respect to each other, and therefore could happen in any order or even at the same time (that is, if this wasn't defined as undefined behaviour by the C++ standard; given that it is, it may output whatever it wants).

»
31 hour(s) ago, # |
  Vote: I like it -22 Vote: I do not like it

The answer lies in the name of the operator.

++x : Pre-increment (increment has high priority)

x++ : Post-increment (increment has low priority)

cout << x++; : First print x then increment

cout << ++x; : First increment then print

Case 1 : cout << x++ + ++x;

  • x++ : Takes the current value of x(5), then increments x to 6.
  • ++x : Increments x to 7, then takes the updated value.
  • Result : 5 + 7 = 12.

Case 2 : cout << ++x + x++;

  • ++x : Increments x from 5 to 6, then takes the updated value (6).
  • x++ : Takes the current value of x (6), then increments x to 7.
  • Result : 6 + 6 = 12.

Case 3 : cout << ++x + ++x;

  • ++x : Increments x from 5 to 6, then takes the updated value (6).
  • ++x : Increments x again from 6 to 7, then takes the updated value (7).
  • Result : 6 + 7 = 13.

Case 4 : cout << ++x + ++x + ++x;

  • ++x : Increments x from 5 to 6, then takes the updated value (6).
  • ++x : Increments x from 6 to 7, then takes the updated value (7).
  • ++x : Increments x from 7 to 8, then takes the updated value (8).
  • Result : 6 + 7 + 8 = 21.

These results are based on the assumption of left-to-right evaluation and no reordering. However, in C++, modifying and accessing a variable multiple times in the same statement can invoke undefined behavior, so the actual result might vary depending on the compiler.

»
31 hour(s) ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

cool quiz now guess what this will output without compiling

    int x=5;
    cout<<x+++x<<endl;
»
31 hour(s) ago, # |
  Vote: I like it -11 Vote: I do not like it

12

12

13

21

++x => Increases then prints

x++ => prints then Increases

when it x++ + ++x => he print it and increase + another increase then print

»
10 hours ago, # |
  Vote: I like it 0 Vote: I do not like it
int x=5;
cout<< x++ + ++x<<endl;//=12,x=7
x=5;
cout<< ++x + x++<<endl;//=12,x=7
x=5;
cout<<++x + ++x<<endl;//=13,x=7
x=5;
cout<<++x + ++x + ++x<<endl;//=21,x=8
»
5 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

1st:5+7=12 2nd:6+6=12 3rd:6+7=13 4th:6+7+8=21

»
3 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

Output would be:

12

12

13

21

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

It`s so beautiful!