-1
int a = 10;
int b = 5;
b += ++a + b++;
a = a++ * ++b;
cout << a;

This is the given problem .I am confused in line 3 where it says b+=++a+b++;.I just want to ask whether we will add b before or after we calculate the statement? Correct output is 253. Please help

Waqar
  • 7,722
  • 3
  • 31
  • 42
  • 2
    The answer depends on which C++ standard your compiler is using. Before C++17, the behaviour is undefined; C++17 resolved that, so that your RHS will be evaluated *completely* before the assignment. See:[What made i = i++ + 1; legal in C++17?](https://stackoverflow.com/q/47702220/10871073) – Adrian Mole Jun 26 '20 at 17:03
  • 7
    The solution is simple: don’t that. Analyzing write-only code is a waste of time. Write clear, concise code. – Pete Becker Jun 26 '20 at 17:05
  • 3
    Echo @PeteBecker. The only correct response is "_Code like this should never be written._". Even if C++17 _does_ completely define which order things happen, that's no reason to make use of that definition. – TripeHound Jun 26 '20 at 17:13
  • 2
    Three answers, and not one mentions either the C++17 standard or sequence points. Hmm. – Adrian Mole Jun 26 '20 at 17:18
  • @AdrianMole: C++17 has been the standard for years, I don't see much point in also discussing legacy versions of C++ for this question. – Mooing Duck Jun 26 '20 at 17:22
  • @MooingDuck Then the answers should *at least* mention the fact that the RHS of the assignment operator will be evaluated first (which is the OP's question, after all). – Adrian Mole Jun 26 '20 at 17:24

6 Answers6

1

Let's see it line by line.

int a = 10;
int b = 5;

Here we have set a to 10 and b to 5.

b += ++a + b++;

When you do ++a, it means you are increasing a immediatly, so that the increased value is used for the current operation. When you do b++, it means that b will be increased AFTER the operation.

In that moment, you are saying b += 11 + 5;. 11 comes from the already increased a and 5 is the value of b, which is only increased after the operation ends.

b += 16 will turn b into 21, since b was 5 before. When the operation ends, you increase 1 in b, because of the b++, resulting in 22.

Here we have set a to 11 and b to 22.

a = a++ * ++b;

The idea is the same, b will be immediatly increased, so we have a = a * 23, resulting 253, since a was 11 before. After the operation, we should add 1 in a, because of the a++, but we WON'T, because we are setting a new value for a and the old value, which would be increased, doesn't exist anymore.

Here we have set a to 253 and b to 23.

cout<<a;

Outputs 253.

Daniel
  • 6,573
  • 5
  • 23
  • 62
1

There are a few rules.

  1. You have to check the associativity of each operator.
  2. You have to take the max possible matching sentinels at a time

To keep it simple, I'll explain it line by line.

  1. int a= 10; --------- Variable a is initialized with a value of 10
  2. int b= 5; ---------- Variable b is initialized with a value of 5
  3. b+=++a+b++;--------- Var a becomes 11 for ++a then 11 will be added with value of b for ++a+b it'll become 16. Then b will be incremented with a value 1 for b++ and will become 6. At last, for b+= *calculated_value* in the end the value of b will be added with 16 and become 22 = 16+6.
  4. a=a++*++b; --------- Variable a will get multiplied with incremented b. Means, first b will be incremented by 1 b++ and become 23, then 11 and 23 will get multiplied a*++b and become 253. After that a will get incremented and become 12 a++. But here is the twist, 253 will override 12 since it'll be the last executing sentinel for this line. i.e, at the end of the execution of this line, var a will be equal to 253.
  5. cout << a; --------- Var a will get printed and you may see 253 on the screen.

Things are a little bit confusing, but always remember that pre-increments i.e, ++var get executed before or you can say in the first place. But post-increments executes later, the variable remains unchanged for the previous operator.

Saptarshi
  • 127
  • 2
  • 6
  • 1
    More accurately, the result of a post-increment expression is the value before the increment. That doesn't tell you, for example, how `b = b++` works. Before C++17 its behavior was undefined. – Pete Becker Jun 26 '20 at 20:02
0

These increment/decrement operators work in the following fashion:

i++ returns the current value of i and then increments i by 1.

++i increments i by 1 then returns the new value of i.

i-- returns the current value of i and then decrements i by 1.

--i decrements i by 1 then returns the new value of i.

Thus, the line b += ++a+b++ first increments a and returns the new value of a, which is 11.

Now we have b += (11)+b++. The current value of b is 5, so b++ returns the value 5 and increments b by 1 --> b += (6)+(5).

Thus, b ends up being incremented by 11+5+1, or 17, so b ends up being equal to 22. In the end, we have a = 11 and b = 22 after this statement.

Telescope
  • 1,748
  • 1
  • 4
  • 19
  • 1
    Wrong! Using clang with the Standard set to C++14 gives this: **warning G325541F5: multiple unsequenced modifications to 'a'** and this: **warning G325541F5: multiple unsequenced modifications to 'b'**. – Adrian Mole Jun 26 '20 at 17:22
0

The precedence of the assignment operator(+=) is the least among the other operators used in the expression. You can check the full table here.

Also the precedence of increment operators is more than that of addition, so first it does the increments, i.e ++a or ++b and then adds them together.

Madhur Vashistha
  • 319
  • 1
  • 2
  • 7
  • While that's true, it doesn't tell you what the result of `b = b++` is. And, by extension, `b += whatever + b++`. That's not a matter of precedence. Prior to C++17 the behavior of both of those expressions was undefined. – Pete Becker Jun 26 '20 at 20:04
0

This is problematic C++ code according to gcc 10.1

$ cat -n tmp.cpp|sed -r 's/^ *//;s/\t/ /'
1 #include <iostream>
2
3 void func()
4 {
5   int a = 10;
6   int b = 5;
7   b += ++a + b++;
8   a = a++ * ++b;
9   std::cout << a;
10 }
$ g++ -Wall -Werror -Wextra -c tmp.cpp
tmp.cpp: In function `void func()':
tmp.cpp:8:5: error: operation on `a' may be undefined [-Werror=sequence-point]
    8 |   a = a++ * ++b;
      |   ~~^~~~~~~~~~~
tmp.cpp:8:5: error: operation on `a' may be undefined [-Werror=sequence-point]
cc1plus: all warnings being treated as errors
Zartaj Majeed
  • 470
  • 1
  • 6
0

Simply put, in c++17, when you have a post-increment operator on the RHS acting on the same variable on the LHS, you apply the increment to the LHS before you apply any of the equality operators.

int a = 10;
int b = 5;
b += ++a + b++; // 6 += 11 + 5   gives b = 22, a = 11
a = a++ * ++b;  // a(=12) = 11 * 23  gives 253 by overwriting the 12
cout << a;      // gives 253