Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a 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 value of ‘a’ will be 11 because the value of ‘x’ gets modified before using it in the expression.
// CPP program to demonstrate pre increment // operator. #include <iostream> using namespace std; int main() { int x = 10, a; a = ++x; cout << "Pre Increment Operation"; // Value of a will change cout << "\na = " << a; // Value of x change before execution of a=++x; cout << "\nx = " << x; return 0; } |
Output :
Pre Increment Operation a = 11 x = 11
Post-increment operator: A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.
Syntax:
a = x++;
Here, suppose the value of ‘x’ is 10 then value of variable ‘b’ will be 10 because old value of ‘x’ is used.
// CPP program to demonstrate pre increment // operator. #include <iostream> using namespace std; 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; } |
Output :
Post Increment Operation a = 10 x = 11
Evaluating post and pre-increment together
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
You may also like:
- https://www.geeksforgeeks.org/difference-between-p-p-and-p/
- https://www.geeksforgeeks.org/c-operator-precedence-associativity/
This article is contributed by Souvik Nandi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- How to delete a range of values from the List using Iterator
- How to create a List with Constructor in C++ STL
- C program to store Student records as Structures and Sort them by Name
- How to delete last element from a List in C++ STL
- Can C++ reference member be declared without being initialized with declaration?
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Some useful C++ tricks for beginners in Competitive Programming
- Replace each node with its Surpasser Count in Linked List
- Comparison of Java with other programming languages
- Print multiples of Unit Digit of Given Number
- Similarities and Differences between Ruby and C language
- Similarities and Differences between Ruby and C++
- Program to copy the contents of one array into another in the reverse order
- Interesting facts about C Language



