C - Unions
Syntax of Union in C
Unions in C are user-defined data types that allow different data members to share the same memory space. The syntax to declare a union is with the union keyword, similar to the way struct is used for structures.
C Union Declaration
A union is defined using the union keyword. For instance:
The declaration above includes a union named "circle" with two members, name and radius. At the same time, two union variables circle1 and circle2 are created.
Different Ways to Define a Union Variable
Union variables can be defined in multiple ways:
Defining Union Variable with Declaration
When you declare a union, you can simultaneously define union variables:
In the example above, stud1 and stud2 are union variables declared alongside the student union.
Defining Union Variable after Declaration
Alternatively, union variables can be defined separately after the union's declaration:
Here, the union variables stud1 and stud2 are defined after the student union declaration.
Accessing Union Members
To access the members of a union, you use the dot (.) operator. If you have a pointer to a union, then you'd use the arrow (->) operator.
Initialization of Union in C
When initializing a union, only the first member can be set using an initializer:
In this example, only the name member of the stud3 union variable is initialized, as it's the first member in the union.
Example of Union
A real-world example is a two-wheeler dealership that has both bicycles and motorcycles. While a motorcycle might have attributes like engine size and mileage, a bicycle might only have attributes like color. However, both might have a price attribute. Using unions, the memory space for these attributes can be optimized:
Size of Union
In C, the size of a union is determined by its largest member due to the fundamental nature of unions. Unions enable multiple data members to share the same memory location, unlike structures where each member has its distinct memory allocation. As a result, when you declare a union with members of varying data types, the compiler calculates the size of the union based on the largest member's size. This approach ensures efficient memory utilization by allocating just enough memory to accommodate the most substantial member. Consequently, unions are particularly valuable when dealing with situations where different data members need to occupy the same memory space at different times, helping conserve memory resources effectively.
Consider a union with an int and a char array of size 32:
Output:
The union size is equivalent to the size of its largest member, in this case, the char array name.
Example 1: C program to find the size of the union
Difference between C Structure and C Union
Feature | Structure | Union |
---|---|---|
Memory | Sum of sizes of all members | Size of the largest member |
Member Access | All members can be accessed simultaneously | Only one member can be accessed at a time |
Initialization | All or specific members can be initialized | Only the first member can be initialized |
FAQs
Q. What is the size of the given union?
A. The size of a union in C is determined by its largest member. For example, if a union contains an integer and an array of 20 integers, the size of the union will be the size of the largest member, which is 80 bytes (20 elements * 4 bytes each). Even if the array consists of similar data elements, it is considered a single entity by the C compiler.
Q. Can we store data in multiple union members at the same time?
A. No, it is not possible to store data in multiple union members simultaneously. A union allows only one member to have a meaningful value at any given time. For instance, if you change the value of one member, it will be reflected in other members that share the same memory location.
Q. What are the applications of unions?
A. Unions find application in scenarios where memory optimization is essential. They enable different data members to share the same memory location, thereby conserving memory space. One common use case is in embedded systems where memory is limited. Unions are also valuable when a program requires direct access to memory, such as dealing with specific hardware registers.