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 4 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.

Srk said:   1 decade ago
Consider value of x is stored in address 500.

So y = z = 500.

If we increment the value of the pointer it will be incremented to point next location where a new integer will be stored.

*z++=500+sizeof (int) *1 == 504.

Same thing happens for y.

Naga Linga Murthy Doddipatla said:   1 decade ago
Here address of x is 500 and value is 30
//value of int takes 4byte
y=&x // y=500
z=y // z=500
*y++ = *z++ //both will incremented by 4bytes
x++ //value incremented by 1

After execution x=31, y=504, z=504.

Anushka said:   9 years ago
This question has some error, as the pointer *y++ and *z++ will point to the value x=30, and 500 is value of y and z respectively not the value of pointer *y and *z, if the statement would be like y++ = z++, then the answer would be option D.

Pranoy Gn said:   1 decade ago
@eneryone

In ++*y the value the pointer is pointing to will be incremented
Bit, in *y++ the address will be incrementedand it is given as 4 bytes for integer and address is 500 given. So
In *y ==> y=500 ==>*y++ ==> y=504
and x++=31

Manu said:   1 decade ago
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=(*z)++;
x++;
printf("x=%d,x add=%u, y=%u, z=%u\n", x,&x, y, z);
/run dis code u.ll get to kw d diff b/w (*Z)++ n *(Z++)

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.

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?

LOIS said:   9 years ago
It's just because the address of x is not 500. You can start by display it after its declaration.

printf ("x=%d \n", &x);

The output will be 2293100. Now the incremented value will be as you have: 2293104.

Rajesh said:   1 decade ago
x=30;
y=& x;=>*y=30.

They said that x address location is 500th and size is 4 bytes.

*y++=*z++=>*y=31.

x++=31;

y and z will jump to next location i.e. 504.

That is why x=31, y=504, z=504.


Post your comments here:

Your comments will be displayed after verification.