union in c

union in c

Union is a derived data type like structure and it also contain different data types of members. so talk about syntax of declaration and accessing members of union is similar to that used in structure. But here keyword union is used in the place of struct.There is main difference between union and structure is the way memory allocation for the members. talk about structure memory, in structure each member has its own memory location. But in the union The members share the common memory location or share the same memory location. when a variable of type union is declared, compiler allocates sufficient memory to store the largest member in the union. so all members share the same memory location, in this we can use only one member at a time. since union also used for save memory

The syntax of declaration of a union is

union union_name
{
datatype member1;
datatype member2;
datatype member3;
……………. }

like structure variable we can also declare union variable, let us take example

union union_name
{
datatype member1;
datatype member2;
datatype member3;
…………….
…………….
};
variable_name;
union union_name variable_name;

In the previous session we know about structure, so we can access the union members using the same syntax used for structures. if we use union variable we can access members of union using the .(dot) operator. we also know about pointer learn previous session. If There is a pointer to union then the members can be access using the arrow(->) operator.

/* programm to accessing union members */
#include<stdio.h>
int
main()
{
union Employee
{
char grade;
int empid;
float salary;
}
emp;
emp.grade='A';
printf("Grade %c\n",emp.grade);
emp.empid=989;
printf("empid %d\n",emp.empid);
emp.salary=15000;
printf("salary %f\n",emp.salary);
return 0;
}

output: Grade A empid 989 salary 15000

Before the printf all values assign to the members such as A assign to grade member, after print 989 assign to empid then print empid then 15000 assign to salary member only one member can hold the value at a time so all member can not be use simultaneously so it is responsibility of programmer to keep track of member that currently hold the value. Note: In this example only one member can hold the value at a time. so in this example one value is assign at a time and print one by one. we can also initialize union variables, but there is a limitation, so we know due to the common memory sharing , members can not hold values simultaneously, so during the initialization only one member can hold the value.

/* program to compare the memory allocate to the union and structure variable */
#include<stdio.h>
struct
stag
{
char ch;
int i;
float f;
};
union utag
{
char ch;
int i;
float f;
};
int main()
{
struct stag svar;
union utag uvar;
printf("size of svar=%u\n",sizeof(svar));
printf("Address of svar: %p \n",&svar);
printf("Address of members : %p, %p, %p \n",&svar.ch,&svar.i,&svar.f);
printf("Size of uvar=%u\n",sizeof(uvar));
printf("Address of uvar: %p \n",&uvar);
printf("Address of members : %p ,%p ,%p \n",&uvar.ch,&uvar.i,&uvar.f);
return 0;
}

output:
size of svar=12
Address of svar:00167FGEC0 , 00167FGEC4, 00167FGEC8
Size of uvar=4
Address of uvar: 00126GFEF0, 00126GFEF0 , 00126GFEF0