For the union declaration below
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
Since i and ch store in the same place, i should now be <binary rep. of 3> concatenated with <binary rep. of 2>. However, it is stored in the reverse order.
printf("%d",i);
gives 515.
Why so??
The result depends on the endianness of the machine you are trying your program on.
It is
515on a little endian machine Intel i686:and is
770on a bin endian machine like Sparc:This happens because the endianess will determine if
ch[0]will be higher order byte (in big endian) or lower order byte (in little endian) ofi.