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 7 of 10.
Rajesh Gowda said:
1 decade ago
I'm gonna explain *y++=*z++
here ++ is the post increment . when u use ++ operator with assignment operator(=), first assignment takes place then the value will be incremented by one. see here y++=z++ is the two step process 1) y=z
2) increment the value of z by one and increment the value of y by one
here ++ is the post increment . when u use ++ operator with assignment operator(=), first assignment takes place then the value will be incremented by one. see here y++=z++ is the two step process 1) y=z
2) increment the value of z by one and increment the value of y by one
Kiran Kumar Y said:
1 decade ago
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
here remember 3 points..
initially v knw d value of x = 30, which is incremented, then x=31.
the address of( *z) z is 500 bcoz z=y and x=&y , when incrementing this the value of integer added to it then 504. similarly yalso 504..
z=y;
*y++=*z++;
x++;
here remember 3 points..
initially v knw d value of x = 30, which is incremented, then x=31.
the address of( *z) z is 500 bcoz z=y and x=&y , when incrementing this the value of integer added to it then 504. similarly yalso 504..
Prathyusha said:
1 decade ago
Here the output when i compiled is is x=31,y=-10,z=-10.++ has higher precedence than *,
So address is incremented to 504 and the value at that address is fetched.....
Is this correct??
So address is incremented to 504 and the value at that address is fetched.....
Is this correct??
Gururaj said:
1 decade ago
@Kishore Mylavarapu
Yes explanation is very good I agree
Thanks.
Yes explanation is very good I agree
Thanks.
Gaurav gupta said:
1 decade ago
*y++ and *z++ means 1st assign the add. of(z=500) then increase it. If it is written like (*y)++ then we put the value of that given ponter then increment will perform.
Raks said:
1 decade ago
Simple concept I have commented the flow just check out.
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; //both pointers r pointing to same value
*y++=*z++; /* z is post incremented hence original value adress 500 is assigned then incremented but, here even we r incrementing y hence it changes from 500 to 504(it is integer type). */
x++; //incremented in the next line it is printed
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; //both pointers r pointing to same value
*y++=*z++; /* z is post incremented hence original value adress 500 is assigned then incremented but, here even we r incrementing y hence it changes from 500 to 504(it is integer type). */
x++; //incremented in the next line it is printed
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
Sadaf Rahman said:
1 decade ago
In a pointer char stores 2 bytes, int stores 4 bytes, long stores 8 bytes.
Hence in above example x=504 and y=504.
Hence in above example x=504 and y=504.
Sundeep katta said:
1 decade ago
Since Z and y have same values in them y.
The statment *y++=*z++ actually assigns the value in z (address of x)to ythus Y++ makes it 500, similarly z++.
The statment *y++=*z++ actually assigns the value in z (address of x)to ythus Y++ makes it 500, similarly z++.
Verma said:
1 decade ago
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504
Manu said:
1 decade ago
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=(*z)++;
x++;
printf("x=%d,x add=%u, y=%u, z=%u\n", x,&x, y, z);
/run dis code u.ll get to kw d diff b/w (*Z)++ n *(Z++)
z=y;
*y++=(*z)++;
x++;
printf("x=%d,x add=%u, y=%u, z=%u\n", x,&x, y, z);
/run dis code u.ll get to kw d diff b/w (*Z)++ n *(Z++)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers