A union is an aggregate of members whose data types can differ.
Members can be scalar variables, arrays, structures, unions, and
pointers to any object. The size of a union is the size of its
longest member plus any padding needed to meet alignment
requirements. All its members occupy the same storage. Unions are
defined with the union keyword, followed by an optional tag,
followed by a union-declaration list in braces. The syntax is:
union [identifier] { union-declaration ... }
Each union-declaration is a type specifier (type keyword, struct
tag, union tag, enum tag, or typedef name) followed by a list of
member declarators:
type-specifier member-declarator,... ;
Each member declarator defines either an ordinary variable or a bit
field:
declarator
or
[declarator] : constant-expression
Once a union is defined, a value can be assigned to any of the
objects declared in the union declaration. For example:
union name {
dvalue;
struct x { int value1; int value2; };
float fvalue;
} alberta;
alberta.dvalue = 3.141596; /*Assigns pi to the union object*/
Here, alberta can hold a double, struct, or float value. The
programmer has responsibility for tracking the current type
contained in the union. The type is maintained until explicitly
changed. An assignment expression can be used to change the type of
value held in the union.