Pages

Union

Union is group of similar or dissimilar data types or elements .it is a user defined data type .it start with the union keyword and terminate with ( ; ) same as structure .union allocates a single memory space to its elements.

Syntax     union <union name>
                {
                 datatype  variable name;     
                 datatype  variable name;
                  }

Program
#include<stdio.h>
#include<conio.h>
union  student
{
char name[20];
int roll , marks;
};
void main()
{
union  student s;
clrscr();
s .name =' h';
printf ("\n your name =%c ",s.name );
 s.roll=1;
 printf ("\n your rollno =%d ",s.roll );
s.marks=100;
 printf ("\n your marks =%d ",s.marks);

}

We can  access the elements of the union with the help of union variable and dot ( . ) operator with element name .we can assign the values to the union elements and retrive the values from the union elements .


Advantage of unioin 

1 it is used for memory saving 
2 Group of elements of similar and dissimilar  data types
3 it is group  of different element s
4 it is  user define data type 






0 comments: