Pre-increment and Post-increment in C/C++
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 either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable. Thus it can be classified into two types:
- Pre-Increment Operator
- Post-Increment Operator
1) 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:
a = ++x;
Here, if the value of ‘x’ is 10 then the value of ‘a’ will be 11 because the value of ‘x’ gets modified before using it in the expression.
CPP
// CPP program to demonstrate pre increment // operator #include <iostream> using namespace std; // Driver Code int main() { int x = 10, a; a = ++x; cout << "Pre Increment Operation" ; // Value of a will change cout << "\na = " << a; // Value of x change does not change after execution of a= ++x , because it is already changed; cout << "\nx = " << x; return 0; } |
Pre Increment Operation a = 11 x = 11
2) 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:
a = x++;
Here, suppose the value of ‘x’ is 10 then the value of variable ‘a’ will be 10 because the old value of ‘x’ is used.
CPP
// CPP program to demonstrate post increment // operator #include <iostream> using namespace std; // Driver Code int main() { int x = 10, a; a = x++; cout << "Post Increment Operation" ; // Value of a will not change cout << "\na = " << a; // Value of x change after execution of a=x++; cout << "\nx = " << x; return 0; } |
Post Increment Operation 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:
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++
// CPP program to demonstrate special // case of post increment operator #include <iostream> using namespace std; int main() { int x = 10; cout << "Value of x before post-incrementing" ; cout << "\nx = " << x; x = x++; cout << "\nValue of x after post-incrementing" ; // Value of a will not change cout << "\nx = " << x; return 0; } |
Output:
Value of x before post-incrementing
x = 10
Value of x after post-incrementing
x = 10
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
Contact Us