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.
Kishore Mylavarapu said:
1 decade ago
y=&x means y stores address of x which is 500 and *y value is 30.
Now z=y which means z stores the value of y.The value of y is 500.
so z==500.
Now *y++=*z++ which implies *y=30 and *z=500(Since, * operator gives you the value of that pointer).
So *y=500 and *z=500.
Now we have to increment(++).Since, the size is 4 bytes..*y=504 and *z=504.
Now x++ which implies x=31.
Now z=y which means z stores the value of y.The value of y is 500.
so z==500.
Now *y++=*z++ which implies *y=30 and *z=500(Since, * operator gives you the value of that pointer).
So *y=500 and *z=500.
Now we have to increment(++).Since, the size is 4 bytes..*y=504 and *z=504.
Now x++ which implies x=31.
Kidsid said:
1 decade ago
Hey guys the correct solution is that
here it uses the concept of precedence of operators like
*y=*z
*y++;
*z++;
in case of *z++
the ++ has higher precedence than *
so that it increment z by 4 than get that value instead u should try
(*y)++;
check it out??
here it uses the concept of precedence of operators like
*y=*z
*y++;
*z++;
in case of *z++
the ++ has higher precedence than *
so that it increment z by 4 than get that value instead u should try
(*y)++;
check it out??
Rajakumar said:
1 decade ago
&x=500
x=3
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++ =504
and x++=31
x=3
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++ =504
and x++=31
Mayur said:
1 decade ago
*y++ means *(y++)
if u want to increase value of y then
(*y)++ is correct syntax....
if u want to increase value of y then
(*y)++ is correct syntax....
Silpi roy said:
1 decade ago
How x++=31?
Akhilesh singh said:
1 decade ago
Given x=30
and x+1=x++
s0 x++=30+1=31;
and x+1=x++
s0 x++=30+1=31;
Doubt said:
1 decade ago
Can anybody explain in detail how *y++ gets the value 504 why not 31 ?
MAK said:
1 decade ago
Initially x=30, y=500 , z=500
*y++=*z++;
'y'& 'z' is address of x. But after executing above step the value in address of 'y' , 'z' is incremented to 31, but it cannot store in same location (Since 30 is stored in address 500), so it will store it next location 504.
x++;
x is incremented to 31,
printf("x=%d, y=%d, z=%d\n", x, y, z);
Now x is 31, y & z is 504
*y++=*z++;
'y'& 'z' is address of x. But after executing above step the value in address of 'y' , 'z' is incremented to 31, but it cannot store in same location (Since 30 is stored in address 500), so it will store it next location 504.
x++;
x is incremented to 31,
printf("x=%d, y=%d, z=%d\n", x, y, z);
Now x is 31, y & z is 504
Aditya Sekhar said:
1 decade ago
It is all dependent on the associativity of unary operators
! ~ ++ -- + - * (type) sizeof Associativity -- right to left
therefore *y++ = *(y++); (From right side ++ comes first )
same *x++ = * (x++) ; (From right to left )
so (y++) == *(504) is assigned (x++) == *(504) value
so it wont reflect in the printf statement
! ~ ++ -- + - * (type) sizeof Associativity -- right to left
therefore *y++ = *(y++); (From right side ++ comes first )
same *x++ = * (x++) ; (From right to left )
so (y++) == *(504) is assigned (x++) == *(504) value
so it wont reflect in the printf statement
Pandey said:
1 decade ago
Concept:.
++ has greater precedence then *.
++ has greater precedence then *.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers