Hello, everyone. I recently started learning C language and I was trying to figure out how unions work. I wanted to try something with it but I can't quite understand the result. For all I know, unions can be used to convert data types since they are using the same memory address but I'm not good with variable types so can someone explain why the code's doing this? What's the relation between "1101004800" and "20,423" ? Why do I get a "0,00000" when I try to display int value as float? What is the effective way to convert these types (double, int, float etc.) with or without using unions? Here's the code and the result. Thank you for your answers.
Code:#include <stdio.h> #include <string.h> typedef union { float fvar; int ivar; }obj; main(){ obj a; a.ivar=20; printf("%f\n",a.fvar); a.fvar=20,423; printf("%d\n",a.ivar); }
If you have other examples in which useful ways of how to use unions are shown, I'd be grateful to see them as well. Have a good day.Code:0.000000 1101004800