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.
PRAVEER said:
6 years ago
It is already given that size is 4.
As it depends on 16byte or 32byte?
As it depends on 16byte or 32byte?
(1)
Brijkishor said:
1 decade ago
First x = 30.
x++ means x= 31.
*y++ means *(y++) =*(z++) it means y = z = 504.
Means only pointer of address variable i.e y and z increase not value.
x++ means x= 31.
*y++ means *(y++) =*(z++) it means y = z = 504.
Means only pointer of address variable i.e y and z increase not value.
Teju said:
1 decade ago
X=30;
X++=31;
Y=&X;
&X=500;
Y=500;
Z=Y=500; SO *Y++=500+4=504 because pointer size is 4 bytes given.
*z++=*y++; *z++,*y++ is same.
So x=31;z=504;y=504.
X++=31;
Y=&X;
&X=500;
Y=500;
Z=Y=500; SO *Y++=500+4=504 because pointer size is 4 bytes given.
*z++=*y++; *z++,*y++ is same.
So x=31;z=504;y=504.
Singh said:
1 decade ago
x = 30.
x++ = 31.
And z++ = 500+sizeof(int) = 504.
Same as y = 504.
x++ = 31.
And z++ = 500+sizeof(int) = 504.
Same as y = 504.
Sudarshan said:
1 decade ago
Here x = 30; y = z = 500;
Then,
*z++ = 504 = *y++;//inc takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Then,
*z++ = 504 = *y++;//inc takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Natwar said:
1 decade ago
@Samiksha.
You wrote that
" The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED! "
But I think you are doing mistake in -ve and +ve addresses.
You may got o/p like this:
x=30, y=-4352852, z=-4352852.
x=31, y=-4352848, z=-4352848.
Please check this,
y=-4352852, z=-4352852.
y=-4352848, z=-4352848.
Value of y is negative.
-4352852 + 4 = -4352848.
If value of y is +ve, then o/p will be like:
y=+4352852, z=+4352852.
y=+4352856, z=+4352856.
You wrote that
" The value of x increases by 1, which is normal variable
But the values of *y and *z are seen to decrease by 4 (bytes)
This happens because the memory addresses are arranges in descending order from top to bottom, so when 500 becomes 496, it actually means that the pointer values has INCREASED! "
But I think you are doing mistake in -ve and +ve addresses.
You may got o/p like this:
x=30, y=-4352852, z=-4352852.
x=31, y=-4352848, z=-4352848.
Please check this,
y=-4352852, z=-4352852.
y=-4352848, z=-4352848.
Value of y is negative.
-4352852 + 4 = -4352848.
If value of y is +ve, then o/p will be like:
y=+4352852, z=+4352852.
y=+4352856, z=+4352856.
Rahul reddy said:
1 decade ago
y* and z* are integer data types.so when ++y* and ++*z represents,
++*y=y+4bytes and ++*z=z+4 because * is address of corresponding variable but not its value as integer normally takes 2 bytes but according to comments it is represented as 4 bytes.
++*y=y+4bytes and ++*z=z+4 because * is address of corresponding variable but not its value as integer normally takes 2 bytes but according to comments it is represented as 4 bytes.
Sourav patra said:
1 decade ago
y=&x; //address of x i.e 500 is assigned to y.
z=y; // pointer y contains address value of x i.e 500 is assigned to z.
*y++=*z++; //first value with in z pointer assigned to y, then z is incremented to next memory location i.e 504. Now y is incremented to next memory location i.e 504.
x++; // value contained by x i.e 30 incremented to next number 31.
printf("x=%d, y=%d, z=%d\n", x, y, z); // values are printed.
z=y; // pointer y contains address value of x i.e 500 is assigned to z.
*y++=*z++; //first value with in z pointer assigned to y, then z is incremented to next memory location i.e 504. Now y is incremented to next memory location i.e 504.
x++; // value contained by x i.e 30 incremented to next number 31.
printf("x=%d, y=%d, z=%d\n", x, y, z); // values are printed.
Saurav pradhan said:
1 decade ago
Since ++ and * have the same precedence and it'll be evaluated from right to left so the answer will be 500 and integer 4 bytes will added to 500 so it will be 504
*y++ = *z++;.
We had assume address of y as 500 and added integer 4 bytes to it.
And evaluated the expression from right to left.
Therefore answer will be 504 for y and z. And x is incremented to 31.
*y++ = *z++;.
We had assume address of y as 500 and added integer 4 bytes to it.
And evaluated the expression from right to left.
Therefore answer will be 504 for y and z. And x is incremented to 31.
James said:
1 decade ago
This is all about precedence, suffix ++ has higher precedence than * and prefix ++. So, *y++ equal to *(y++). Since ++ is suffix, So first *y and then y++.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers