Union in C Language
Union in C
C provides us a special data type and that data type is called Union. Union can store many data types in the same memory location. We can create variables of different data types inside a Union. Unions are similar to structures in C language. The difference between these is that every member of the structure occupies a separate memory location and the size of them all is different while in the Union the members occupy the same memory space and the size depends on the size of the largest member.
Union
- Union is the collection of different data types
- If we want to use Union ,use of ‘union’ keyword is mandatory.
- Every declaration done inside a union is called member.
- A single memory location is shared among the members of the Union.
- The biggest in size among the members decides the size of the Union.
Defining a union
To define a Union, ‘union’ keyword is used. It is same as defining a structure.
The basic syntax of union is as follows :
union union_name { data_type var1; data_type var2; .. .. data_type varn; };
First we define union keyword with a unique union name. After this we define variables of different data types in curly brackets.
union Employee { int emp_id; char emp_name[30]; float salary; };
Now in the above example we can see that the name of the union is taken as ‘Employee’ and there are three members of the Union- emp_id, emp_name and salary. Now variables can be declared for the union and every variable will have three members which will have a shared memory location.
Union variable declaration
There are two types to declare variables for Union:-.- When the definition of Union is written then Union variables can be declared along with it.
- The variable of union can also be declared inside the main() function.
Syntax for Union Variable outside of Union Definition
union Union_name { member 1; member 2; ------------------ ------------------ member n; }Union_variable(s);
union Teacher { int teach_id; char teach_name[30]; }t1,t2;
Syntax for Union Variable in main() Function
union Union_name { member 1; member 2; ------------------ ------------------ member n; }; int main() { union Union_name variable_name; }
Example for Union Variable in main() Function
union Teacher { int teach_id; char teach_name[30]; }; int main() { union Teacher info; }
Union working with size(sizeof)
As we have already discussed, The members of the union are stored in the same memory location and the size of the whole union equals to the largest member of the Union.
So, when we find the size of the union it will return the size of the largest member of the union.
Let’s take an example for reference:
#include<stdio.h> #include<string.h> union Teacher { int teach_id; char teach_name[20]; float salary; }; int main() { union Teacher info; printf( "Size of Teacher id is : %d bytes\n", sizeof(info.teach_id)); // size of teach_id printf( "Size of Teacher name : %d bytes\n", sizeof(info.teach_name)); // size of teach_name printf( "Size of Teacher salary is : %d bytes\n", sizeof(info.salary)); // size of salary printf( "Size of Teacher union : %d bytes", sizeof(info)); // size of Teacher return 0; }
Output :
Size of Teacher id is : 4 bytes Size of Teacher name : 20 bytes Size of Teacher salary is : 4 bytes Size of Teacher union : 20 bytes
Accessing Members using pointer Operator
#include<stdio.h> #include<string.h> union Teacher { int teach_id; char teach_name[30]; float salary; }; int main() { union Teacher info; union Teacher *ptr; ptr = &info; ptr->teach_id = 34; strcpy( ptr->teach_name, "Sanya"); ptr->salary = 20000.00; printf( "Teacher id is : %d\n", ptr->teach_id); printf( "Teacher name is %s\n", ptr->teach_name); printf( "Teacher salary is : %f", ptr->salary); return 0; }
Output :
Teacher id is : 1184645120 Teacher name is Teacher salary is : 20000.000000
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment