Last Updated : 27 May, 2025
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:
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:
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++
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;
}
x = 11 Assigning x to a with Pre Increment Operation a = 11 x = 11Post-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:
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++
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;
}
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++
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;
}
Value of x : x = 10 Assigning x to x with Post-incrementation x = 10 //No change in value of x
Evaluating Post and Pre-Increment TogetherNote: 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.
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.
Pre and Post Increment Operators in C++
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4