Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   union problem (http://www.go4expert.com/forums/union-problem-t28926/)

manish007g 22Aug2012 16:06

union problem
int main()
union a
int i;
char ch[2];
union a z={512};
printf("%d %d",z.ch[0],z.ch[1]);
return 0;

Output is 0 2 but how? please explain ..

xpi0t0s 23Aug2012 05:02

Re: union problem
Think how 512 is stored in memory. Did you get 0x02 and 0x00 (thus forming the 16-bit number 0x0200)? That's where the 0 and 2 come from.

Best not rely on this though. This is going to be very compiler and platform dependent - completely unportable.

All times are GMT +5.5. The time now is 01:26.