C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Discussion:
92 comments Page 2 of 10.
Naveena said:
9 years ago
I understood the answer but I have some confusion why not the answer is 31,31,31 because if we remove the line *y++=*z++; it is printing *y=30,*z=30, then if we increase *y++ it has to be 31 as *y is 30 and also for *z but why it incrementing address, why not value but while printing *y why it is not printing 500 but it is 30?
Pease give me the clear explanation.
Pease give me the clear explanation.
LOIS said:
9 years ago
It's just because the address of x is not 500. You can start by display it after its declaration.
printf ("x=%d \n", &x);
The output will be 2293100. Now the incremented value will be as you have: 2293104.
printf ("x=%d \n", &x);
The output will be 2293100. Now the incremented value will be as you have: 2293104.
Anushka said:
9 years ago
This question has some error, as the pointer *y++ and *z++ will point to the value x=30, and 500 is value of y and z respectively not the value of pointer *y and *z, if the statement would be like y++ = z++, then the answer would be option D.
Shobhit Kumar said:
9 years ago
On compiling the answer came as x = 31, y = 2293104, z = 2293104.
Deep said:
9 years ago
See in *y++ first y++ is performed and gives 500+4 504 then it performs *(y++) ie *(504) so y value changes to 504.
Siri said:
9 years ago
How to write 500? Please tell me.
Pallvi said:
10 years ago
Why post incr returning incremented value?
Tejaswi Gunti said:
10 years ago
In case of *y++ and *z++ the precedence is from right to left. So first the address of y and z is incremented and then the value is assigned because of the indirection (*).
int size is 4 bytes so address becomes address +4. So the answer is D.
i.e, x=31 (since x++).
y = 504 (If we take the base address as 500).
z = 504 (If we take base address as 500).
int size is 4 bytes so address becomes address +4. So the answer is D.
i.e, x=31 (since x++).
y = 504 (If we take the base address as 500).
z = 504 (If we take base address as 500).
(1)
Pari said:
10 years ago
Can anybody clear my doubt regarding post increment and pre increment in C? Cause here, post increment is taking place hence first value should be returned the it have to be increment. But here incremented value is returning?
Gabor said:
10 years ago
@Bhargav:
The x=30 is saved from 500-503, because the '30' (integer) value is 4 byte size. So the next free address is 504.
The x=30 is saved from 500-503, because the '30' (integer) value is 4 byte size. So the next free address is 504.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers