What is the difference between “++i” and “i++” in C?

What is the difference between “++i” and “i++” in C?

**Answer:**
In C, both “++i” and “i++” are increment operators used to increase the value of the variable “i” by 1. However, they differ in their behavior with respect to the expression’s value and the order of evaluation.

1. **”++i” (Pre-increment):** This operator increments the value of “i” before the current value is used in the expression. It returns the incremented value of “i”. For example:

int i = 5;
int result = ++i; // Increment i first, then assign to result
// Now, i = 6, result = 6

2. **”i++” (Post-increment):** This operator increments the value of “i” after the current value is used in the expression. It returns the original value of “i” before the increment. For example:

int i = 5;
int result = i++; // Use i first, then increment
// Now, i = 6, result = 5

In summary, “++i” performs the increment operation before using the variable, while “i++” performs the increment operation after using the variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.