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

C++CServer Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

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.

Updated on 30-Jul-2019 22:30:25