C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x=4, y, z;
    y = --x;
    z = x--;
    printf("%d, %d, %d\n", x, y, z);
    return 0;
}
4, 3, 3
4, 3, 2
3, 3, 2
2, 3, 3
Answer: Option
Explanation:

Step 1: int x=4, y, z; here variable x, y, z are declared as an integer type and variable x is initialized to 4.
Step 2: y = --x; becomes y = 3; because (--x) is pre-decrement operator.
Step 3: z = x--; becomes z = 3;. In the next step variable x becomes 2, because (x--) is post-decrement operator.
Step 4: printf("%d, %d, %d\n", x, y, z); Hence it prints "2, 3, 3".

Discussion:
26 comments Page 3 of 3.

Ashanth said:   1 decade ago
How X=2? Please explain.

Jeeth said:   1 decade ago
Can anyone explain this.

Gunu said:   9 years ago
I didn't get how x=2?

Abhi said:   7 years ago
Please Explain me.

Ninad said:   9 years ago
Thanks @Raj.

Devi said:   6 years ago
Thanks @Raj.


Post your comments here:

Your comments will be displayed after verification.