22 Jan C – Unions
Structures in C language allow you to store data of different types, whereas Unions allow storing different datatypes but in the same memory location.
Unions vs Structures
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; float b; double c; char d; }; int main( ) { printf("Size of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 |
Size of union = 8 |
Now, let us modify the above example. We have changed the variable d:
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; float b; double c; char d[10]; }; int main( ) { printf("Size of union = %d", sizeof(union Demo)); return 0; } |
Output
1 2 3 |
Size of union = 16 |
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:
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