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 1 of 10.
Ramanathan said:
1 year ago
@All.
Here y and z point to the same location and that contains the address of var x and dereferences to value 30.
Then post increment ++ has higher precedence than * this the address of y gets incremented but still. Not assigned to ptr var y as it's post decrement. The updated address is shown only while printing it at the final statement so the address remains the same now similar concept applies to *z++ thus no changes happen inside this statement next x gets incremented this at the final statement we get the incremented value of x i e 31 and the updated address of y and z i.e the postfix operation has come into light only after its use after post increment line.
i.e
Say int x=30 now if I printf("%d",x++);
I still get 30 as it isn't assigned (incremented value ) to var x. It is only after this block finishes execution.
Now again printf("%d",x) and you get the incremented value i.e 31.
Here y and z point to the same location and that contains the address of var x and dereferences to value 30.
Then post increment ++ has higher precedence than * this the address of y gets incremented but still. Not assigned to ptr var y as it's post decrement. The updated address is shown only while printing it at the final statement so the address remains the same now similar concept applies to *z++ thus no changes happen inside this statement next x gets incremented this at the final statement we get the incremented value of x i e 31 and the updated address of y and z i.e the postfix operation has come into light only after its use after post increment line.
i.e
Say int x=30 now if I printf("%d",x++);
I still get 30 as it isn't assigned (incremented value ) to var x. It is only after this block finishes execution.
Now again printf("%d",x) and you get the incremented value i.e 31.
(1)
Sahibpreet Singh said:
2 years ago
*(value of address) and increment/decrement operators lie in the same level of precedence. Their precedence level proceeds in right to left manner.
So, if ++ is to the left of * then * will be evaluated first.
Similarly, (in this case) ++ is to the right of * so y++ is evaluated first. *y++ is same as *(y++) and ++*y is same as ++(*y).
So, if ++ is to the left of * then * will be evaluated first.
Similarly, (in this case) ++ is to the right of * so y++ is evaluated first. *y++ is same as *(y++) and ++*y is same as ++(*y).
(8)
Chidananda said:
5 years ago
Since they mentioned integer is 4 bytes. If we increment from 500 address it will take next 4th address i.e is 504.
(7)
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)
Nitis said:
6 years ago
Address size will be 2 byte, then how it can be 504?
It should 502.
It should 502.
(2)
Shubham said:
6 years ago
#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; // initialization z by 500
*y++=*z++; // pointer size is 4 byte when we use the increment operater at that time pointer incremented by the pointer size.
x++;
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; // initialization z by 500
*y++=*z++; // pointer size is 4 byte when we use the increment operater at that time pointer incremented by the pointer size.
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
(4)
Priya kulshreshtha said:
8 years ago
& deals with the address of the variable mark that point.
So here y=&x
&x means address of x
What is the address of x?
It is 500
So y=500
Point it that the value stored in y is the address of another variable y.
Again it says
Z=y
Z=500
Now,
*y++=*z++
Let's break the term
* in front of the pointer returns the value
Meaning *y point to some other variable
That is
Y=&x
So *y=30
And it needs to be incremented because of
*y++
But if we consider the right side of y assignment
There is *z++
That stores the address 500
And since it has to be incremented
And here address increment must be from 4
That is 504
So here y=z= incremented value that is 504.
So here y=&x
&x means address of x
What is the address of x?
It is 500
So y=500
Point it that the value stored in y is the address of another variable y.
Again it says
Z=y
Z=500
Now,
*y++=*z++
Let's break the term
* in front of the pointer returns the value
Meaning *y point to some other variable
That is
Y=&x
So *y=30
And it needs to be incremented because of
*y++
But if we consider the right side of y assignment
There is *z++
That stores the address 500
And since it has to be incremented
And here address increment must be from 4
That is 504
So here y=z= incremented value that is 504.
(6)
Tpr said:
8 years ago
*y++=*z++;.
Is above statement means 504=504?
Is above statement means 504=504?
(2)
Prince said:
8 years ago
Can anyone tell depending on the size of integer *y++ value will increment by 4?
Ramesh chand rebari said:
8 years ago
Here the value of x =30;
We will consider that the address of x is 500;
And very integer have size 4 byte, so that *y++=*z++ are equal having both address value 500 and incremented with one time and value of x is also incremented by one that's why x having 31 and y=z having 504.
We will consider that the address of x is 500;
And very integer have size 4 byte, so that *y++=*z++ are equal having both address value 500 and incremented with one time and value of x is also incremented by one that's why x having 31 and y=z having 504.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers