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;
}
x=31, y=502, z=502
x=31, y=500, z=500
x=31, y=498, z=498
x=31, y=504, z=504
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
92 comments Page 8 of 10.

Lekha said:   1 decade ago
Its very simple.
X=30 /*assume base address is 500*/
z=y=&x;
z=y=500;
pointer increment to this address will be increment.
*y++=*z++=504;
int will be 4byte.
X will be incremented.
X=31.
Prints that values x=31,y=504,z=504.

Divya said:   1 decade ago
Hi @abc

y++ increments the value of y
*y++ increments the pointer value
for eg: if y=3 then y++=3+1=4
*y=3 then *y++=3+4=7
for pointers it will be incremented by 4

Pandey said:   1 decade ago
Concept:.

++ has greater precedence then *.

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

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

Doubt said:   1 decade ago
Can anybody explain in detail how *y++ gets the value 504 why not 31 ?

Akhilesh singh said:   1 decade ago
Given x=30
and x+1=x++
s0 x++=30+1=31;

Silpi roy said:   1 decade ago
How x++=31?

Mayur said:   1 decade ago
*y++ means *(y++)
if u want to increase value of y then
(*y)++ is correct syntax....

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


Post your comments here:

Your comments will be displayed after verification.