- 1

Union

Why union in c with multiples members like union u { int a; float b; }; The second membre behaves in stranger manner?

29th Dec 2018, 1:14 AM
Vinicius
Vinicius - avatar
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.
29th Dec 2018, 2:09 AM
Gordon
Gordon - avatar
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);
29th Dec 2018, 10:07 PM
Vinicius
Vinicius - avatar
0
Oh, yes. Now I understood. Thank you
29th Dec 2018, 10:10 PM
Vinicius
Vinicius - avatar
0
union, test.
30th Jul 2022, 3:49 PM
Kunjam indrapriyanka
0
Val{ Int int_num; Float f1_num; }; Union val info; Union Val ptr= Info;
31st May 2023, 12:08 PM
Dhanvi Sompalli