Updated July 7, 2023
Introduction to C Union
Union is a user-defined data type in c, it allows storing of different data elements of different data types to be stored in the same memory location. It provides an efficient way of utilizing the memory, as only one union member can be accessed at any given time. Therefore, the size of a union at any point in time would be equal to the size of its largest element. Though a union is like a structure, the main difference is that in a structure a separated memory is allotted for each member of the structure whereas in the union it’s a shared memory which is equivalent of the size of the largest member.
Syntax:
A union can be defined as below.
union UnionName
{
UMember1;
UMember2;
UMember3;
} UnionReference;
The C Union members can be accessed using the reference ‘UnionReference’. union is a keyword.
Example of struct and union in memory allocation.
Let us demonstrate the difference between struct and union in memory allocation.
Code:
#include <stdio.h>
struct samp_structure
{
char name1[30];
int e_id;
float e_slry;
char name2[60];
}s;
union s_union
{
char name1[30];
int e_id;
float saly;
}u;
int main()
{
printf("size of structure :%ld bytes \n", sizeof(s));
printf("size of union : %ld bytes\n", sizeof(u));
return 0;
}
Output:
Examples of C Union
Let us see the implementation with the help of the examples mentioned below:
Example #1
This is an example to define a C union and accessing its members.
Code:
#include <stdio.h>
#include <string.h>
union test {
int tint;
float tf;
char tstr[20];
};
int main( ) {
union test t;
t.tint = 100;
printf( "record.i : %d\n", t.tint);
t.tf= 200.5;
printf( "record.f : %f\n", t.tf);
strcpy( t.tstr, "Test");
printf( "record.str : %s\n", t.tstr);
return 0;
}
Output:
Example #2
Below is the same code as above except that the print statements have been rearranged.
Code:
#include <stdio.h>
#include <string.h>
union test {
int tint;
float tf;
char tstr[20];
};
int main( ) {
union test t;
t.tint = 100;
t.tf = 200.5;
strcpy( t.tstr, "Test");
printf( "record.i : %d\n", t.tint);
printf( "record.f : %f\n", t.tf);
printf( "record.str : %s\n", t.tstr);
return 0;
}
Output:
If you look at the output carefully, you can see that garbage values have been assigned for int and float because the string was allotted the memory, at last, i.e. since the members share the memory in a union the member whose value is currently stored will have access to the memory.
Example #3
Anonymous union is a union that is not named, hence they can be used inside any nested structure or unions. The members of the anonymous union can be directly accessed within the scope of their definition. Similarly, Anonymous structure can be used inside an anonymous union.
Syntax of Anonymous union and structure
// Anonymous union example
union
{
char anoUChar;
int anoUNum;
};
// Anonymous structure example
struct
{
char anoSChar;
int anoSNum;
};
Example of anonymous struct union inside a struct.
Code:
#include<stdio.h>
struct testscope
{
// Anonymous union
union
{
char testChar;
int testNum;
int testNum2;
};
};
int main()
{
struct testscope ts;
ts.testNum = 65;
// Note that members of union are accessed directly
printf("testchar = %c, testnum = %d,testnum2 = %d", ts.testChar, ts.testNum,ts.testNum2);
return 0;
}
Output:
The testchar has been assigned the value ‘A’ because the recent value in the union memory is 65 which was assigned to testNum, hence the corresponding ASCII character is printed.
Example of anonymous struct inside a union.
Code:
#include<stdio.h>
union testscope
{
// Anonymous union
struct
{
char testChar;
int testNum;
int testNum2;
};
};
int main()
{
union testscope ts;
ts.testNum = 65;
ts.testChar='V';
//Note: The members of struct are accessed directly
printf("testchar = %c, testnum = %d,testnum2 = %d", ts.testChar, ts.testNum,ts.testNum2);
return 0;
}
Output:
Conclusion
Thus, the union helps in managing memory efficiently. The drawback of the union is that only the last entered value to the union will only be available. It should be used when members of the union need not be available to be accessed at the same time.
Recommended Articles
This is a guide to C Union. Here we discuss the introduction, syntax, and different examples of c union with code implementation. You may also look at the following articles to learn more –