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 4 of 10.
Prince said:
8 years ago
Can anyone tell depending on the size of integer *y++ value will increment by 4?
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?
Murali said:
1 decade ago
@Basant Sukdev.
Good explanation clear view. Really I understood on first on first step. Others also done well.
Good explanation clear view. Really I understood on first on first step. Others also done well.
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