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 3 of 10.
Nikhil said:
1 decade ago
Ya I know the answer is correct.
But please can anyone suggest me when we consider 16 bit compiler and when we consider 32 bit in questions. Because it is not mentioning there about 16 bit or 32 bit.
I faces this problem many times in my all test.
But please can anyone suggest me when we consider 16 bit compiler and when we consider 32 bit in questions. Because it is not mentioning there about 16 bit or 32 bit.
I faces this problem many times in my all test.
Abid Ali said:
1 decade ago
first of all x = 30.
x++ means x= 31.
Since the precedence of *(pointer operator) is lower than ++(post increment), *y++ means *(y++). Post increment shows its effect after execution of the very statement. So y gets incremented to 504 but its effect is ignored while executing the statement.
To better understand this take this example.
int main()
{
int i[] = {3, 5};
int *p = i;
int j = --*p++;
printf("j = %d\n\n", j);
return 0;
}
In this the statement --*p++ can be interpreted as --(*(p++)).
x++ means x= 31.
Since the precedence of *(pointer operator) is lower than ++(post increment), *y++ means *(y++). Post increment shows its effect after execution of the very statement. So y gets incremented to 504 but its effect is ignored while executing the statement.
To better understand this take this example.
int main()
{
int i[] = {3, 5};
int *p = i;
int j = --*p++;
printf("j = %d\n\n", j);
return 0;
}
In this the statement --*p++ can be interpreted as --(*(p++)).
Sabbul said:
1 decade ago
Here y=z; means both has same address i.e 500.
Now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
Note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
So...value at z is assigned to y(i.e nothing but 30).
And after that both address is incremented by 4 bytes(according to the question).
So finally,
x++;
Becomes 31 and z, y becomes 504, 504.
Now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"
Note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.
So...value at z is assigned to y(i.e nothing but 30).
And after that both address is incremented by 4 bytes(according to the question).
So finally,
x++;
Becomes 31 and z, y becomes 504, 504.
Sangram said:
1 decade ago
Here x = 30; y = z = 500;
Then,
*z++ = 504 = *y++;//as it takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Then,
*z++ = 504 = *y++;//as it takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Monika said:
1 decade ago
I got the answer, but after (*y++=*z++;) this expression what will the why & z point to? and what is the value at the address 504?
Please provide me full explanation.
Please provide me full explanation.
Gautam said:
1 decade ago
@Vikash.
Because '=' has right to left associativity means left operand should be unambiguous but in case of y++ = z++ the '=' can't find an unambiguous operand(++ or y++).
Because '=' has right to left associativity means left operand should be unambiguous but in case of y++ = z++ the '=' can't find an unambiguous operand(++ or y++).
Vikash said:
1 decade ago
Someone please tell.
Why compiler doesn't execute
y++ = z++;
while execute *y++ = *z++;
Why compiler doesn't execute
y++ = z++;
while execute *y++ = *z++;
Sekar said:
1 decade ago
1. The address of x which is assumed as 500 and size is 4 byte passed to pointer y.
2. The address stored in y is passed to pointer z.
3. Then both y and z get post increment that is 500 to 504. because the size is 4 byte.
4. the value of x again incremented to 30 to 31;
ANS is = x = 31, y = 504, and z = 504.
2. The address stored in y is passed to pointer z.
3. Then both y and z get post increment that is 500 to 504. because the size is 4 byte.
4. the value of x again incremented to 30 to 31;
ANS is = x = 31, y = 504, and z = 504.
Vaibhav said:
1 decade ago
*y++ = *z++;
Means 504 = 504;
Can anyone tell me how can I assign a value to a value?
Is this not an L-value ERROR!
Means 504 = 504;
Can anyone tell me how can I assign a value to a value?
Is this not an L-value ERROR!
Pari said:
10 years ago
Can anybody clear my doubt regarding post increment and pre increment in C? Cause here, post increment is taking place hence first value should be returned the it have to be increment. But here incremented value is returning?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers