- 1
Union
Why union in c with multiples members like union u { int a; float b; }; The second membre behaves in stranger manner?
7 Réponses
+ 2
The second member b, shows a lot of trailing zeroes after decimal dot when printed, because it's datatype is float.
0
For example. When I print int_num and fl_num in this code I get a strange number in console. Only variablrle str print corretly ("hello"). Why?
union val {
int int_num;
float fl_num;
char str[20];
};
union val test;
test.int_num = 123;
test.fl_num = 98.76;
strcpy(test.str, "hello");
printf("%d\n", test.int_num);
printf("%f\n", test.fl_num);
printf("%s\n", test.str);
0
Oh, yes. Now I understood. Thank you
0
union, test.
0
Val{
Int int_num;
Float f1_num;
};
Union val info;
Union Val ptr=
Info;