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 2 of 3.

Richa said:   1 decade ago
No, when various conditions are written in any statement then only we go from right to left.

Sayli said:   9 years ago
How x becomes 2? What is the function of post increment operator?

Anomi said:   7 years ago
When will z become true? After the completion of 1 complete loop?

Rohit C said:   4 years ago
Thanks for explaining this in detail @Mohammed Shahbaz, @Shweta.
(1)

Rohit said:   1 decade ago
Please how's the value of x will be 3. I think it would be 4.

Abbas said:   7 years ago
Anyone, please Explain me to understand.

Kavipriya said:   7 years ago
I didn't get it. Please explain.

Soumen das said:   9 years ago
How it is 2 3 3? Explain me.

Vensi said:   9 years ago
How the value of x becomes?

SANATH said:   10 years ago
Can any one say how 2 got?


Post your comments here:

Your comments will be displayed after verification.