Open In App

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

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol.
The increment operator can be classified into two types:

  • Pre-Increment Operator
  • Post-Increment Operator

Pre and Post Increment Operator in C/C++

The pre and post-increment operators in C perform the same task, but have different operator precedence. Understanding pre-increment and post-increment operators is essential for controlling variable values.

Pre-increment operator

A pre-increment operator is used to increment the value of a variable before using it in an expression. In the Pre-Increment, value is first incremented and then used inside the expression.
Syntax:  

C
 a = ++x;

Here, if the value of ‘x’ is 10 then the value of ‘a’ will be 11 because the value of ‘x’ is incremented before assigning it to 'a'.

The above expression can be as equivalent to

C++
x=x+1; 
a=x;

Pre-Increment Operator in C Program

CPP
#include <iostream>
using namespace std;

int main()
{
    int x = 10, a;

    a = ++x;
    cout<<"x = "<<x<<endl; 
    cout << "Assigning x to a with Pre Increment Operation";
    // 'a' will have the new value of x
    cout << "\na = " << a;

    // Value of x changes before 
    // execution of a = x , 
    cout << "\nx = " << x;

    return 0;
}

Output
x = 11
Assigning x to a with Pre Increment Operation
a = 11
x = 11

Post-increment operator

A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented. 
Syntax:  

C++
 a = x++;

Here, suppose the value of ‘x’ is 10 then the value of variable ‘a’ will be 10 because the values of x is assigned to 'a' before the incrementation operation, thus the old value of ‘x’ is used.

The above expression can be considered equivalent to

C++
a = x; 
x = x + 1;

Post-Increment Operator in C Program

CPP
#include <iostream>
using namespace std;

int main()
{
    int x = 10, a;

    a = x++;
    cout<<"x = "<<x<<endl; 
    cout << "Assigning x to a with Post Incrementation";
    //'a' will have the old value of x
    cout << "\na = " << a;

    // Value of x change changes 
    //after execution of a =x
    cout << "\nx = " << x;

    return 0;
}

Output
x = 11
Assigning x to a with Post Incrementation
a = 10
x = 11

Special Case for Post-increment operator: If we assign the post-incremented value to the same variable then the value of that variable will not get incremented i.e. it will remain the same like it was before.

Syntax:

C++
a = a++;

Here, if the value of ‘x’ is 10 then the value of ‘a’ will be 10 because the value of ‘x’ gets assigned to the post-incremented value of 'x'.

C++
#include <iostream>
using namespace std;

int main()
{
    int x = 10;
    cout << "Value of x :";
    cout << " x = " << x;
    x = x++;
    cout << "\nAssigning x to x with Post-incrementation";

    // Value of a will not change
    cout << "\nx = " << x<<" //No change in value of x";
    return 0;
}

Output
Value of x : x = 10
Assigning x to x with Post-incrementation
x = 10 //No change in value of x

Note: This special case is only with post-increment and post-decrement operators, while the pre-increment and pre-decrement operators works normal in this case.

Evaluating Post and Pre-Increment Together

The precedence of postfix ++ is more than prefix ++ and their associativity is also different. Associativity of prefix ++ is right to left and associativity of postfix ++ is left to right.  

  • Associativity of postfix ++ is left to right.
  • Associativity of prefix ++ is right to left.

Pre and Post Increment Operators in C++
Article Tags :
Practice Tags :

Similar Reads