#include<stdio.h> #include<string.h> union Data { float f; char str; }; int main( ) { union Data data; data.f=11111; data.str='S'; printf( "data.f : %f\n", data.f); printf( "data.str : %f\n", data.str); return 0; } it will print both.but when you interchange str and f then the string is lost.why it so.
This is because of the union defination. This union is of 4 byes. str is its 1st byte. And f uses all 4 bytes.
So, in 1st case - you 1st allocate the all 44 bytes for f andthen replace only the 1st byte by str i.e why both remains.
But when you interchange them, 1st byte is allocated to str, but when f is defined it replaces all 4 bytes and the str is changed accordingly to f(which is not desired by you).