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.

Singh said:   1 decade ago
x = 30.
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.

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.

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.

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.

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

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.

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++)).

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.

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.


Post your comments here:

Your comments will be displayed after verification.