C - Language Theory
Unions
Unions are user-defined datatype, which is used to store a collection of diffterent types of data, just like a structure.
- Unions are same like structures but only difference is it used less memory alloaction compared to structure.
- The keyword 'Union' is used to declare the structure in C.
- Variables inside the structure are called members of the Union.
It occupies less memory because it occupies the size of the largest member only. [In union, all members share the same memory location.]
Union Declaration
//member variable 1
//member variable 2
//member variable 3
}[union_variable];
char name[40];
int id[10];
float salary[50];
}[union_variable];
Note: In above declaration 'employe' is name of the structure. And name, salary and id are members of the union in this case.
Declaration of Union Variables
union employee { char name[40]; int id[10]; float salary[50]; } main () { struct employee2 } or struct employee { char name[40]; int id[10]; float salary[50]; } el,e2; |