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;
}
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.
Devi said:
6 years ago
Thanks @Raj.
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.
(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.
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.
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
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.
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)
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;
}
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;
}
Rohit C said:
4 years ago
Thanks for explaining this in detail @Mohammed Shahbaz, @Shweta.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers