# Pre-increment and Post-increment Operators in C/C++

C/C++: Pre-increment and Post-increment Operators: Here, we are going to learn about the Pre-increment (++a) and Post-increment (a++) operators in C/C++ with their usages, examples, and differences between them.
Submitted by IncludeHelp, on June 01, 2020

In C and C++ programming language, there is an operator known as an increment operator which is represented by ++. And it is used to increase the value of the variable by 1.

There are two types of Increment operator,

1. Pre-increment Operator
2. Post-increment Operator

## Pre-increment Operator

The Pre-increment operator increases the value of the variable by 1 before using it in the expression, i.e. the value is incremented before the expression is evaluated.

Syntax:

```    ++a
```

Example:

```    Input:
a = 10;
b = ++a;

Output:
a = 11
b = 11
```

In the expression b=++a, ++a will be evaluated first thus, the value of a will be 11 and then assignment operation will be performed.

Program to demonstrate the example of pre-increment

```#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 0;

cout << "Before the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;

//Pre-increment operation
b = ++a;

cout << "After the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;

return 0;
}
```

Output:

```Before the operation...
a: 10, b: 0
After the operation...
a: 11, b: 11
```

## Post-increment Operator

The Post-increment operator increases the value of the variable by 1 after using it in the expression, i.e. the value is incremented after the expression is evaluated.

Syntax:

```    a++
```

Example:

```    Input:
a = 10;
b = a++;

Output:
a = 11
b = 10
```

In the expression b=a++, a++ will be evaluated after assigning the value of a to the b. Thus, the value of b will be 10 and then a++ will be evaluated and then the value of a will be 11.

Program to demonstrate the example of post-increment

```#include <iostream>
using namespace std;

int main()
{
int a = 10;
int b = 0;

cout << "Before the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;

//Pre-increment operation
b = a++;

cout << "After the operation..." << endl;
cout << "a: " << a << ", b: " << b << endl;

return 0;
}
```

Output:

```Before the operation...
a: 10, b: 0
After the operation...
a: 11, b: 10
```

## Difference between pre-increment and post-increment operators

Since both are used to increase the value of the variable by 1. But based on the above discussion and examples, the difference between pre-increment and post-increment operators is very simple. The pre-increment increased the value before the expression is evaluated and the post-increment increases the value after the expression is evaluated.

Program to demonstrate the use of pre and post increment operators

```#include <iostream>
using namespace std;

int main()
{
int A = 10, B = 20, C = 0;

C = A++ + ++B * 10 + B++;
cout << A << "," << B << "," << C;

return 0;
}
```

Output:

```11, 22, 241
```

Explanation:

Based on the above discussion, the express will be evaluated like this,

```
Values are,
A = 10
B = 20
C = 0

Expression is,
C   = A++ + ++B * 10 + B++;

C   = 10 + 21 * 10 + 21
= 10 +210 +21
= 241

Finally, the values of A, B and C will be,
A = 11
B = 22
C = 241
```

Note: Compiler dependency may be there.