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 1 of 3.
Jeeth said:
1 decade ago
Can anyone explain this.
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 ?
Can we consider it like this z=3, y=3, a=2 please explain ?
Richa said:
1 decade ago
No, when various conditions are written in any statement then only we go from right to left.
Rohit said:
1 decade ago
Please how's the value of x will be 3. I think it would be 4.
Ashanth said:
1 decade ago
How X=2? Please explain.
SANATH said:
10 years ago
Can any one say how 2 got?
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.
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.
Sayli said:
9 years ago
How x becomes 2? What is the function of post increment operator?
Vensi said:
9 years ago
How the value of x becomes?
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers