
Good question! The difference between i++ and ++i lies in how they increment the value of i and when the increment takes place.
i++ is called the post-increment operator. This means that the current value of i is used in the expression, and then it is increased by 1 after that.
For example, if i is 5, then using i++ in an expression would first use 5, and then i becomes 6 afterwards.
#include
int main() {
int i = 5;
printf("%d", i++); // Output: 5
printf("\n%d", i); // Output: 6
return 0;
} On the other hand, ++i is called the pre-increment operator. This means i is incremented by 1 first, and then the new value is used in the expression.
So if i is 5, ++i would increase it to 6 first, and the expression would use this new value (6).
#include
int main() {
int i = 5;
printf("%d", ++i); // Output: 6
printf("\n%d", i); // Output: 6
return 0;
} To summarize:
i++: Use current value, then increment.++i: Increment first, then use new value.
Hope this helps! If you have any more questions, feel free to ask.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer