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:

  1. Simple Declaration
  2. Union within Structure
  3. Initialization of Union

1. Simple Declaration
In general terms, the composition of a union may be defined as:

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:

where, tag is optional in this situation.

Example:

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:

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:

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.

Program in C that reads radius of a circle and calculates its area

The below program in C will accepts the radius of a circle from the user and calculates its area and displays the calculated result to the user on the screen.

Explanation
Step 1: Read the radius
Step 2: Calculate the area by applying the formula: Area = (pi)*r2 = 3.14 * r2

Output:

Multi-Dimensional Arrays in C Programming Language

Let’s understand the concept of multi-dimensional arrays in C programming language.

Although arrays with more than two dimensions are not commonly used, C does allow any number of dimensions to be declared. This is done by listing maximum size of all dimensions of the array. For example, the declaration:

declares a three-dimensional array. The first element in the array is designated as a[0][0][0] and the last element as a[3][9][5].

Thus, generally speaking, multidimensional arrays in C programming language are defined in much the same manner as one-dimensional arrays, except that a separate pair of square brackets are required for each subscript. Hence, a two-dimensional array requires two pairs of square brackets [][], a three-dimensional array will requie three pairs of square brackets [][][], and so on.

In general terms, a multidimensional array definition in C programming language can be written as:

storage-class data-type array[expression 1][expression 2]...[expression n];

where storage-class refers to the storage class of the array,
data-type is its data type,
array is the name of the multidimensional array, and
expression 1, expression 2, ..., expression n are positive valued integer expressions that indicate the number of array elements associated with each subscript.

Remember that the storage-class is optional. The default values are automatic for arrays that are defined inside of a function, and external for arrays defined outside of a function.