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


Here we will see what is the pre-increment and post-increment in C or C++. The pre-increment and post-increment both are increment operators. But they have little differences.

The pre-increment operator increments the value of a variable at first, then sends the assign it to some other variable, but in the case of postincrement, it at first assign to a variable, then increase the value.

Example

#include<iostream>
using namespace std;
main() {
   int x, y, z;
   x = 10;
   y = 10;
   z = ++x; //z will hold 11
   cout << "Z: " << z << endl;
   z = y++; //z will hold 10, then y will be 11
   cout << "Z: " << z << " and y is: " << y << endl;
}

Output

Z: 11
Z: 10 and y is: 11

The precedence of post increment is more than precedence of pre increment, and their associativity is also different. The associativity of pre increment is right to left, of post increment is left to right.

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements