/*
icici's comment is correct. The classic example is creating a union
where either a number may be integer or float. In the example below
numtype is typedef'ed to be either integer or float. It's up to the
programmer to control what type is stored. Trying to output as an
integer when the last assigned value to the union was float yields
garbage.
Another real world example is:
typedef union personit {
int age;
struct {
int month;
int day;
int year;
} personrec;
In the above declaration, the personrec union holds either a single integer
representing the person's age or a structure holding the person's month,
day, and year.
Regards,
Ed
*/
#include <stdio.h>
typedef union typeit {
int intnum;
float fltnum;
} numtype;
int main()
{
numtype intoflt;
intoflt.intnum=54;
printf("integer is %d\n", intoflt.intnum);
intoflt.fltnum=3.1416;
printf("float is %f\n", intoflt.fltnum);
/* garbage from here on*/
printf("float is %d\n", intoflt.fltnum);
printf("integer is %d\n", intoflt.intnum);
}