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 5 of 10.
Divyaradhakrishnan said:
1 decade ago
In post increment the value will be first assigned and then only it will get incremented and here how the value of X is printed as 31. Since I am non csc I can't understand any one explain in basic.
BSP said:
1 decade ago
*y++=*z++.
The above statement execute as:
Value of z(500) is initialize to y(500), and the value of y(500) is incremented to 504, and then the value of z will get increment i.e. 500 to 504.
The above statement execute as:
Value of z(500) is initialize to y(500), and the value of y(500) is incremented to 504, and then the value of z will get increment i.e. 500 to 504.
Prathyusha said:
1 decade ago
Here the output when i compiled is is x=31,y=-10,z=-10.++ has higher precedence than *,
So address is incremented to 504 and the value at that address is fetched.....
Is this correct??
So address is incremented to 504 and the value at that address is fetched.....
Is this correct??
Cpverma said:
1 decade ago
y++ n *y++ is not same......
But in case of
*y++=*z++
*(indirection operator) have no effect becoz they are pointing the same value.so it will cancel out...
like and behave like y++=z++
But in case of
*y++=*z++
*(indirection operator) have no effect becoz they are pointing the same value.so it will cancel out...
like and behave like y++=z++
Garima said:
1 decade ago
x++ means increment of 1 in x value. So x=31.
Same as *y++ and *z++ but difference is integer x contain 4 byte size so the value of *y++ and *z++ is 504 because x address is 500.
Same as *y++ and *z++ but difference is integer x contain 4 byte size so the value of *y++ and *z++ is 504 because x address is 500.
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++).
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.
Gaurav gupta said:
1 decade ago
*y++ and *z++ means 1st assign the add. of(z=500) then increase it. If it is written like (*y)++ then we put the value of that given ponter then increment will perform.
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
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
Teju said:
1 decade ago
X=30;
X++=31;
Y=&X;
&X=500;
Y=500;
Z=Y=500; SO *Y++=500+4=504 because pointer size is 4 bytes given.
*z++=*y++; *z++,*y++ is same.
So x=31;z=504;y=504.
X++=31;
Y=&X;
&X=500;
Y=500;
Z=Y=500; SO *Y++=500+4=504 because pointer size is 4 bytes given.
*z++=*y++; *z++,*y++ is same.
So x=31;z=504;y=504.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers