Unions in C Programming Language
Unions in C programming language (like structures) contains members whose individual data type may differ from one another. However, the members within a union all share the same storage area within the computer’s memory whereas each member within a structure is assigned its own unique storage area.
Thus, unions in C are used to conserve memory. They are useful for applications involving multiple members, where values need not be assigned to all the members at any one time.
Within a union in C, the bookkeeping required to store members whose data types are different (having different memory requirements) is handled automatically by the compiler. However, the user must keep a track of what type of information is stored at any given time. An attempt to access the wrong type of information will produce meaningless results.
Defining or Declaration of Union in C:
The defining or declaration of union in C can get easily understood by keeping in mind the following three points:
- Simple Declaration
- Union within Structure
- Initialization of Union
1. Simple Declaration
In general terms, the composition of a union may be defined as:
union tag{ member 1; member 2; ... member n; };
where union
is a required keyword, and the other terms have the same meaning as in a structure definition.
Individual union variables can then be declared as:
storage-class union tag variable 1, variable 2, ..., variable n;
where storage-class
is an optional storage class specifier,
union
is a required keyword,
tag
is the name that appeared in the union definition or declaration, and
variable 1, variable 2, ..., variable n
are union variables of the type tag
The above two declaration may also be combined. Thus, we can write:
union tag{ member 1; member 2; ... member n; }variable 1, variable 2, ..., variable n;
where, tag
is optional in this situation.
Example:
union id{ char color[12]; int size; }shirt, blouse;
In the above example, we have two union variables. shirt
and blouse
of type id
. Each variable can represent either a 12-character string (color) or an integer quantity (size) at any one time.
The 12-character string will require more storage area within the compiler’s memory than the integer quantity. Therefore, a block of memory large enough for the 12-character string will be allocated to each union variable. Tge compiler will automatically distinguish between the 12-character array and the integer quantity within the given block of memory, as required.
2. Union within Structure
A union in C may be a member of a structure, and a structure may be a member of a union in C. Moreover, structures and unions may be freely mixed with arrays.
Example:
union id{ char color[12]; int size; }; struct clothes{ char manufacturer[20]; float price; union id description; }shirt, blouse;
In the above example, shirt
and blouse are structure variables of type clothes
. Each variable will contain the following members: a string (manufacturer), a floating point quantity (price) and a union (description).
Also, the union (description) itself may represent either a string (color) or an integer quantity (size).
Another way to declare the structure variables shirt
and blouse
is to combine the preceding two declarations as follows:
struct clothes{ char manufacturer[20]; float price; union { char color[12]; int size; }description; }shirt, blouse;
3. Initialization of Union:
A union variable can be initialized provided its storage-class is either external
or static
. However, remember that only one member of a union in C can be assigned a value at any given time. Most compilers will accept an initial value for only one union member, and they will assign this value to the first member within the union.
Processing of Unions in C:
An individual union member in C can be accessed in the same manner as an individual structure member, using the operator period (.). Thus, if variable
is a union variable, then variable.member
refers to a member of the union.
Also, usually in all other respects, unions are processed in the same manner, and with the same restrictions, as structures.
Thanks a lot for writing such a great article. This article on unions in C solved my purpose. It is great to see such a useful resource related to C Programming online. Keep going. Great site to learn C
wow super thanq very much