QUOTE(harshakirans @ 1 Sep, 2007 - 10:52 AM)

QUOTE(Bench @ 31 Aug, 2007 - 10:14 AM)

Put simply, its undefined behaviour. You may not assign a value to one member of a union and then expect to read that value from another union member. (Though there are some notable exceptions when using unsigned char to examine the bit representation of another member)
Here the value is assigned to y (y={100})
and x.a is initialized to 100
x.b to hello
x.c to 21.5
wel y behavoiur may be undefined but wat happened to values in x.
CAn u please explain in detail.
As someone else already mentioned, all variables in a union occupy the same space in memory. Which means that when you use one, other variables in that union are effectively invalid. (The compiler won't stop you accessing them, but they will be invalid as far as their content is concerned)
When you assign 'x.c' a value of 21.5, you've overwritten or invalidated some or all of what was in the union before. After this, any attempt to read x.a or x.b could result in anything happening. Also, the size of a union is always equal to the size of that union's largest member. Other, smaller members of the union are often "padded" in memory so that they are still usable. but there is no set requirement for how or where this padding may exist, so there is no way to predict how union members may affect each another.
Again, the only exception to all this is when you combine a variable, and an array of unsigned char, whose length is equal to the size of that variable, then you can reliably use the array of unsigned char to examine the bit-pattern of that variable.
Edit-Corrected a typoThis post has been edited by Bench: 1 Sep, 2007 - 03:56 AM