A union is a client characterized information type which contains factors of various information types. Or on the other hand, we can say that the association is accumulation of unique sort of components or information. The idea of association is taken from the structure so the sentence structure of association is the same as structure. Association can be characterized utilizing catchphrase “association” as demonstrated as follows.
Association in C
Characterizing Union
Sentence structure
union name_of_union
{
member 1;
member 2;
. . . . .
member n;
};
Model
union student
{
int rollno;
float mark;
char gender;
};
In structure, every part gets discrete memory for capacity while in association every one of the individuals shares the same stockpiling area.
As every one of the individuals shares same stockpiling so we can take a shot at just a single part at once. The compiler apportions absolute memory equivalent to the part having biggest size.
In the above model, the imprint has biggest size of 4 bytes so the all-out memory allotted for a variable of association understudy type will be 4 bytes.
Announcing Union Variables
We can announce the variable of association similarly we accomplish for structure. For instance, on the off chance that we need to proclaim factors for the above association, at that point it tends to be done in the following manner.
union student s1,s2;
Variable can likewise be announced at the time of characterizing the association by simply composing the variable name in the middle of shutting props and semicolon.
Getting to Union Members
We can get to a patron by utilizing spot administrator (.). Keep in mind that, get to just that variable whose worth is at present put away. Consider beneath model.
s1.rollno = 20;
s1.mark = 90.0;
printf("%d",s1.rollno);
The above code will give incorrect yield. This is on the grounds that we have most as of late sored esteem in the imprint and we are getting to roll.
At the point when the worth is doled out to a part then the worth that was doled out to some other part before is lost.
Introducing Union
Association can be introduced at the hour of the announcement of factors. In any case, just first part can be instated and every single other part can be introduced later on by either doling out worth or perusing an incentive from the console. Consider beneath model.
union student s1 = {25}; //valid
union student s1 = {80.5}; //will give undesired or erroneous output
In the above model, the primary proclamation is right while the second explanation will give undesired yield since we are introducing the second part mark (glide kind) of association understudy.
Let’s make one straightforward program under the idea of association in C.
#include<stdio.h>
void main()
{
union student
{
int rollno;
float mark;
char gender;
}s1;
s1.rollno=20;
printf("Roll No:%d",s1.rollno);
s1.mark=90.5;
printf("nMark:%f",s1.mark);
s1.gender='m';
printf("nGender:%c",s1.gender);
}
Output
Roll No.20
Mark : 90.50000
Gender : m
Clarification
The above program is simple, still in the event that you are confronting any issue, at that point remark underneath.
This is about the association in C. On the off chance that you discover any error or something missing in above instructional exercise, at that point please notice it in the remarks.