Union in C
Union is a
collection of variables of different data types. Union is useful for representing
logically related information of different data types.
In union all members share one common memory space. Compiler allocates memory space such that it can accommodate largest member of the union.
be declared using union keyword.
Item is a union having two members declared inside it.
union can be declared using union keyword and union name.
SyntaxIn above example union Item having two members.
union union_name Var1,Var2,Var3;
union Item I1, I2, I3;
Here largest memory occupied is 4 bytes so 4 bytes allocated to each union variable
I1, I2 and I3.