Program Explanation
Program Explanation
#include <stdio.h>
int main() {
int x = 10, y = 20;
int result;
// Pre-increment
result = ++x;
printf("After pre-incrementing, x = %d and result = %d\n", x,
result);
// Post-increment
result = y++;
printf("After post-incrementing, y = %d and result = %d\n", y,
result);
// Pre-decrement
result = --x;
printf("After pre-decrementing, x = %d and result = %d\n", x,
result);
// Post-decrement
result = y--;
printf("After post-decrementing, y = %d and result = %d\n", y,
result);
return 0;
}
result = ++x;
Pre-increment means the value of x is incremented first, and then
the incremented value is assigned to result.
So, x is incremented from 10 to 11.
result is assigned the value of x, which is now 11.
The second printf prints:
After pre-incrementing, x = 11 and result = 11
3. Post-Increment:
result = y++;
Post-increment means the value of y is assigned to result first, and
then y is incremented.
result is assigned the value of y, which is 20 (the current value of y).
Then, y is incremented from 20 to 21.
The third printf prints:
After post-incrementing, y = 21 and result = 20
4. Pre-Decrement:
result = --x;
Pre-decrement means the value of x is decremented first, and then
the decremented value is assigned to result.
So, x is decremented from 11 to 10.
result is assigned the value of x, which is now 10.
The fourth printf prints:
After pre-decrementing, x = 10 and result = 10
5. Post-Decrement:
result = y--;
Post-decrement means the value of y is assigned to result first, and
then y is decremented.
result is assigned the value of y, which is 21 (the current value of y).
Then, y is decremented from 21 to 20.
The fifth printf prints:
After post-decrementing, y = 20 and result = 21
Final Output:
The complete output of this program will be: