Union

 

Union

Union is a group of similar and dissimilar data types or elements. It is a user defined data type. Union starts with 'union' keyword and terminate with semicolon same as structure. Union allocates a single memory to its elements. All the elements of the union share the single memory. To access the elements of union we can create a union variable. It is same as structure.

The major difference between the union and structure is that in structure different members use different memory locations whereas in union all members of union share the same memory location.
Syntax: -
union < union name>
{
<data type> <variable name> ;
<data type> <variable name>;
};
E.g: -
union student
{
char name [20];
 int rollno, marks;
};
Union memory representation:- As we know that union allocate largest memory of its element and it allocate a single memory to all it elements.
.
As shown in the figure and Example




union student
{
char name[20];
int roll, marks;
};
Memory representation

Name




















0    1   2   3 …………………………………………………………18 19

roll<---------------------------------------------------------------->

marks<------------------------------------------------------------>


As shown in the example and figure the union allocate 20 bytes memory for name, roll and marks, which is the largest or higher memory in the union.

WAP to create union ,which can store student detail like student's name, rollno and marks

#include<stdio.h>
#include<conio.h>
union student
{                       
char name[20];
 int rollno,marks;
};
void main()
{
union student s;
 clrscr();
printf("Enter Employ name=");
scanf("%s", &s.name);
 printf("\n Your name=%s",s.name);
 printf("Enter your Rollno.=");
 scanf("%d",&s.rollno);
printf("\n your Rollno. Is =%d",s.rollno);
printf("Enter your Marks=");
 scanf("%d",&s.marks);
printf("\n your marks Is =%d",s.marks);
getch();
}

Initialization of union

We cannot initialization the union same as the structure because. It use the single memory location and all the elements use the same memory.

#include<stdio.h>

#include<conio.h>
union student
{                       
char name;
 int rollno,marks,
};
void main()
{
union student s;
 clrscr();
s.name=’h’;
printf("\n Your name=%c",s.name);
s.rollno=1;
printf("\n your Rollno. Is =%d",s.rollno);
s.marks=89;
printf("\n your marks Is =%d",s.marks);
getch();
}

Union Array

We can also create the array of the union same as structure for storing the detail of more than one student.

Write a program a enter the detail of five students  : their name, rollno and marks using union.

#include<stdio.h>
#include<conio.h>
union student
{                       
char name[20];
 int rollno,marks;
};
void main()
{
int i;
union student s[5];
 clrscr();
for(i=0;i<5;i++)
{
printf("Enter Employ name=");
scanf("%s",&s[i].name);
 printf("\n Your name=%s",s[i].name);
 printf("Enter your Rollno.=");
 scanf("%d",&s[i].rollno);
printf("\n your Rollno. is =%d",s[i].rollno);
printf("Enter your Marks=");
 scanf("%d",&s[i].marks);
printf("\n your marks is =%d",s[i].marks);
}
getch();
}

To check the size of the union

To check the size of the union we can use sizeof() operator. It tells us how much memory is occupied by the union elements.

As shown in the example below:

#include<stdio.h>
#include<conio.h>
union emp
{                       
char name[15];
 int age;
float salary;

};
void main()
{
union emp e;
 clrscr();
printf(“the size of the unio=%d”,sizeof(e));
getch();
        
}



Difference between structure and union
Structure
union
Structute start with ‘struct’ keyword.
Union start with ‘union’ keyword.
All elements of the structure use different memory locations for storing the data.
All elements of the union use single memory locations for storing the data.
It allocates different memory to each element.
It allocates single memory. All the elements share the same memory.
It is commonly used in most of the applications.
It is not commonly used
Syntax
struct <struct name>
{
<data type> <variable>;
};
Syntax
union <union name>
{
<data type> <variable>;
};

1 comments:

  1. Thanks for sharing the best information and suggestions, it is very nice and very useful to us. I appreciate the work that you have shared in this post. Keep sharing these types of articles here.linux vps server

    ReplyDelete