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

Shweta said:   4 years ago
Its simple!
Understand this carefully.

First let me tell you what is post decrementer and pre decrementer.

Post-Decrementation operator first compares the value of x and later on decrement the value of x.
Let's take same example.

#include<stdio.h>
int main()
{

int x=4, y, z;

y=--x;
z=x--;
printf("%d%d%d\n", x,y,z);
return 0;
}


Here,

int x=4, y, z; ( the value of x is 4 and also give the values to the y and z)

y=--x; ( this is called pre decrementation operator as - - lies before x that is why it is called pre )
z=x--; ( this is called post decrementation operator as - - lies after value x that is why it is called post )

So, when

Int x=4, y, z; is compiled the value of x is 4

y=--x; when the compiler comes here it simply first decrement the value of x and then the value of x becomes 3 when the decrementation is done the compiler simply give the value y to 3 as the condition says we have to put the value of y as soon as the decrementation is done. ( Note that x us decrementing first and not putting the value of y first because this method is called pre decrementation operator)


z=x--; in the above statement the value of y becomes 3 because the value of x becomes 3 now, Here the method x- - is called post decrementation operator in this the compiler first compare the x- - to the z= and the value of x is 3 because of the above statement here it will put the value of z as 3 first ( that means it is comparing z= to x- - first ) and then the x - - operation is done. That means if the value of x was 3 now performing x- - it will become 2.

That is why the answer says x=2 y=3 and z=3.
(2)

Mohammed shahbaz said:   5 years ago
Here is a clear explanation,

We know that ++x and x++ are pre and post-increment operators, In the case of ++x, first, the value is incremented and then assigned to any variable,

And in the case of x++, it is vice versa.

Now coming to the question,
clearly it states that x is initially equal to 4 but when it goes to y, it first gets decremented as(4-1) and that equivalent value is stored to y, (y becomes equal to 3).

Again the decremented value of x(3) goes to z and it is stored as equal to 3 because it is a post decrementation operator.

But it is clear that when it goes to z , it again gets decremented by 1 and becomes (3-1).
therefore, finally, the value of x becomes 2.

Shweta said:   4 years ago
It's simple!
Understand this carefully.

First, let me tell you what is post decrementer and pre decrementer.

Post-Decrementation operator first compares the value of x and later on decrement the value of x.
Let's take the same example.

#include<stdio.h>
int main()
{
int x=4, y, z;
y= "x;
printf("%d%d%d\n", x,y,z);
return 0;
}

Raj said:   9 years ago
Y =--x in this case x is decremented first and then after the value of x is assigned to y. So x becomes 3 and y is also 3.

Z =x-- here first the value of x is assigned to z first and then it is decremented so z is 3 and x becomes 3 - 1 = 2.

Siri said:   8 years ago
y=--x means first x is decremented and then assign x value to y.

So, here x=4, x is decremented so x= 3 then assign x to y hence y=3.z=x-- means first x value is assigned to z and then x value is decremented hence z=3 and x=2.

Hafiz Basam said:   7 years ago
You are right @Azeema, @Aswini, @Gayathri.

y = --x so y=3 this 3 is assigned to x. Now x=3.
z = x-- so z=3 because it is a post-decrement.

After performing the operation x=2.
So, finally, x=2 y=3 z=3.

Joan said:   6 years ago
y= - - x
(x is reduced to 3 and y=3)
z = x- -

(Here it is post decrement. The decrement happens after the assignment of z. z=3)
(x gets decremented to 2).

Printing x,y,z we get 2,3,3.

Azeema,Aswini,Gayathri said:   9 years ago
@All.

y = --x so y=3 this 3 is assigned to x. Now x=3.
z = x-- so z=3 because it is a post-decrement.

After performing the operation x=2
So, finally, x=2 y=3 z=3.

Punit lohia said:   1 decade ago
In case of functions the variables are operated from right to left so

Can we consider it like this z=3, y=3, a=2 please explain ?

Bhaggu said:   9 years ago
After excution the value is done with inc/dec operation in the case of post, in that case how the value become as 2?


Post your comments here:

Your comments will be displayed after verification.