What does the operation c=a+++b mean in C/C++?

C++CServer Side ProgrammingProgramming

Let us consider in C or C++, there is a statement like:

c = a+++b;

Then what is the meaning of this line?

Well, Let the a and b are holding 2 and 5 respectively. this expression can be taken as two different types.

  • c = (a++) + b
  • c = a + (++b)

There are post increment operator, as well as pre increment operator. It depends on how they are used.

There are two basic concepts. The precedence and the associativity. Now if we check the expression from left to right, so the result will be these two.

  • c = (a++) + b → 2 + 5 = 7
  • c = a + (++b) → 2 + 6 = 8

Now let us check which option is taken by the compiler-

Example Code

#include <iostream>
using namespace std;
main() {
   int a = 2, b = 5;
   int c;
   c = a+++b;
   cout << "C is : " << c;
}

Output

C is : 7

Here the first option is taken.

raja
Published on 09-Apr-2019 09:34:46
Advertisements