What is Union?
- A Union is a collection of variable of different data type under a single union name.
- Union member takes same memory space.
- The variable are called member of the Union.
- The Union are called user defined data type.
Syntax of Union
union union_name
{
data_type member_variable1;
data_type member_variable2;
………………………………………..
data_type member_variableN;
} s1; //global union variable declaration
OR
union union_name union variable; // local union variable declaration
Create a Union
union abc
{
int rollno;
float marks;
char name[20];
} s1; //global union variable declaration
OR
union abc s1; // local union variable declaration
union abc s1,s2,s3; //Multi union variable declaration
Memory Allocation in a Union
Here, we know that according to the properties of Union.Union takes the same memory location for all the members in union.So the largest member variable of a union store the value of other members.
Here, we take the three members rollno, marks and name that takes 2 byte, 4 byte and 20 byte. Because int take 2 byte, float take 4 byte and char takes 1 byte (array size of 20 ) 1*20=20 bytes.
So the largest member variable takes 20 byte. And all the member used this space.
Accessing of Union
Syntax :
union_variable_name.union_member_name
By using dot (.) operator or period operator or member operator.
Here, union_variable_name refers to the name of a union type variable and union_member_name refers to the name of a member within the union.
Example of Union
//Union is Created
union abc
{
int rollno;
float per;
char name[20];
};
// Initialization a Union
void main()
{
union abc s1;
clrscr();
printf("Enter the Value of Union members\n");
printf("Enter the Roll No.\n");
scanf("%d",&s1.rollno);
printf("Enter the Percentage\n");
scanf("%f",&s1.per);
printf("Enter the Name of the Student\n");
scanf("%s",s1.name);
// Accessing Union member
printf("the Roll No.is = %d\n",s1.rollno);
printf("the Percentage is = %f\n",s1.per);
printf("the Name of the Student is = %s\n",s1.name);
getch();
}
Output:
Now, As we know that union member use the same memory space for the all member with largest memory. Here 20 is largest space by the member and when we enter the integer value then rollno uses that space after we entered the float value, integer value replaced by the float percentage. After that we entered the string value, float replaced by the string value Name. and all the three value now hold the string value name. if we want to show the particular value then we print the value just after the value entered otherwise other member takes the space of that value.
Example of Union
//Union is Created
union abc
{
int rollno;
float per;
char name[20];
};
// Initialization a Union
void main()
{
union abc s1;
clrscr();
printf("Enter the Value of Union members\n");
printf("Enter the Roll No.\n");
scanf("%d",&s1.rollno);
printf("the Roll No.is = %d\n",s1.rollno);
printf("Enter the Percentage\n");
scanf("%f",&s1.per);
printf("the Percentage is = %f\n",s1.per);
printf("Enter the Name of the Student\n");
scanf("%s",s1.name);
printf("the Name of the Student is = %s\n",s1.name);
getch();
}
Output:
Enter the Value of Union members
Enter the Roll No.
1
the Roll No. is =1
Enter the Percentage
67
the Percentage is =67
Enter the Name
Ashish
the Name is = Ashish
Difference b/w Structure and Union in C
Structure | Union |
The separate memory location is allotted to each member of the 'structure'. | All members of the 'union' share the same memory location. |
struct struct_name { type element1; type element2; . . } variable1, variable2, ...; | union union_name { type element1; type element2; . . } variable1, variable2, ...; |
struct abc { int a; float b; char c[20]; } variable1; | union abc { int a; float b; char c[20]; } variable1; |
Size of Structure= sum of
size of all the data members. For the Above structure abc total memory taken by the structure is = int+float+char*20= 2+4+1*20=26. Size of Structure= sum of size of all the data members=26. | Size of Union=size of the largest members. For the Above union abc total memory taken by the union is = int+float+char*20= 2+4+1*20=26. but it takes only 20 byte because largest member has taken 20 byte only. Size of Union=size of the largest members=20. |
Stores distinct values for all the members. | Stores same value for all the members. |
A 'structure' stores multiple values, of the different members, of the 'structure'. | A 'union' stores a single value at a time for all members. |
Provide single way to view each memory location. | Provide multiple way to to view same memory location. |