22 Jan C – Unions
In this lesson, we will learn how to understand Unions in C. We will cover the following concepts:
- What are Unions in C
- Structures vs Unions
- Create a Union with examples
- Access Union Members with examples
What are Unions
Unions in C are a special data type that allows you to store different data types in the same memory location.
Unions are often used in situations where memory efficiency is critical, such as in embedded systems, communication protocols, and hardware interfacing.
Key concepts related to Unions:
- Memory Sharing: Unions are useful when you need to work with different data types but only need to store one type at a time.
- Size: The size of a union is determined by the size of its largest member.
- Access: You can only access the value of the member last assigned. Previous values are overwritten.
Unions vs Structures
Structures in C language allow you to store data of different types, whereas Unions allow storing different datatypes but in the same memory location.
The Unions and Structures in C language are user-defined datatypes. Let us see the difference:
- Definition
Structures in C allow you to store data of different types. The memory for each declared variable in a Structure is allocated to each variable member.
Unions in C allow you to store data of different types, but the memory for the declared variables in a Union is allocated to the largest memory. - Size
The size of the Structure is calculated by adding the sum of its members. Therefore, the size would be greater than or equal to the sum of each member’s size.
The size of the Union is not hard to calculate. It is the size of its largest member. - How to access members?
In Structures, the individual members can be accessed at a time.
In Union, you can access only one member at a time. - Keyword
To define a Structure, use the keyword struct.
To define a Union, use the keyword union.
Create a Union in C
Let us understand the Union concept with an example to define a union. To define a Union, use the union keyword:
1 2 3 4 5 6 7 8 9 |
union Demo { int a; float b; int c; char d[15]; }; |
Above,
- The name of the union is Demo,
- The union members are a, b, c, and d
Let us now see an example and see how many bytes of memory will get allocated in the below program. The largest member of the Union here is double c therefore 8 bytes of memory get allocated and the result would be 8:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> #include <string.h> union Demo { int a; // 4 bytes float b; // 4 bytes double c; // 8 bytes char d; // 1 byte }; int main( ) { printf("Size of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 |
Size of union = 8 |
Above, we have used the sizeof operator in C. It is used to determine the size, in bytes, of a data type or object, Here, we are using it to get the size of union.
Now, let us modify the above example. We have changed the variable d to 20.
Now, the largest member is the char d[20], which is 20 bytes. However, for the alignment requirement of the largest type, double i.e. 8 bytes, the total size padded up to the nearest multiple of the alignment size. Therefore, the output will be 20 + 4 i.e., 24:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> #include <string.h> union Demo { int a; // 4 bytes float b; // 4 bytes double c; // 8 bytes char d[20]; }; int main( ) { printf("Size of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 |
Size of union = 24 |
Above,
- The 20 bytes are for char d[20].
- The 4 bytes of padding is added to make the total size a multiple of 8 bytes (aligning to double’s requirement).
Access Union Members
We will see how to access union members in C Language. For this purpose, use the member access operator (.). Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> union Demo { int a; float b; double c; char d[20]; }; int main( ) { union Demo demo; demo.a = 5; demo.b = 15.6; demo.c = 34.657; strcpy( demo.d, "The demo text!!"); printf( "data.a : %d\n", demo.a); printf( "data.b : %f\n", demo.b); printf( "data.c : %lf\n", demo.c); printf( "data.d : %s\n", demo.d); printf("\nSize of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 4 5 6 7 8 |
data.a : 543516756 data.b : 0.000000 data.c : 5571034366794630322135937434626951168230212563454228113734616102011041449752900712574740169652682723413333557377735149468294617668989253486912245200289114995955931421199044587060284523022604633258887036503843569935356041277472768.000000 data.d : The demo text!! Size of union = 24 |
In the above example, the largest member of the Union is char d[20], therefore the bytes of memory get allocated and size 24 can be seen for the largest d variable. Since it is Union, not a Structure, therefore the largest memory occupied by the variable will get considered for the memory allocation. The rest of the variable values are corrupted.
This can be fixed by working on one variable at a time as shown below. Here, demo is declared as a variable of type union Demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> #include <string.h> union Demo { int a; float b; double c; char d[20]; }; int main( ) { union Demo demo; demo.a = 5; printf( "data.a : %d\n", demo.a); demo.b = 15.6; printf( "data.b : %f\n", demo.b); demo.c = 34.657; printf( "data.c : %lf\n", demo.c); strcpy( demo.d, "The demo text!!"); printf( "data.d : %s\n", demo.d); printf("\nSize of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 4 5 6 7 8 |
data.a : 5 data.b : 15.600000 data.c : 34.657000 data.d : The demo text!! Size of union = 24 |
In the above example, the corrupted values are now fixed. Rest, the size of the Union is the same, as expected.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments