We are assigning 100 to member integer x of value as defined type union number in the following program. Then we are printing both members, and we are seeing that value.y has a value due to union.
When I ran this program, I saw the following:
According to that members of union share the same storage space;
1-) Is the size of storage 4bytes(int) + 8bytes(double) = 12bytes = 96bits? Or is it just 8bytes(double) = 64bits?
2-) Why is member x equal to 0(zero) when assigning any data to member y?
3-) Can we calculate member y when assigning any data to member x? If It can be done, how? How can we know where bits of value.x and value.y in the 96bits-field or 64bits-field?
4-) In my study book, when assigning 100 to member x, the output of member y is showed as -92559592117433136000000000000000000000000000000000 0000000000000.000000 So why doesn't the values in member y in which the picture above and my study book have the same value?
Code:
#include <stdio.h>
#include <stdlib.h>
union number {
int x;
double y;
};
int main()
{
union number value;
value.x = 100;
printf("Put a value in the integer member\n"
"and print both members.\n\n"
"int: %d\n\n"
"double: %f\n\n\n\n",value.x,value.y);
value.y = 100.0;
printf("Put a value in the floating member\n"
"and print both members.\n\n"
"int: %d\n\n"
"double: %f\n\n\n\n",value.x,value.y);
return 0;
}