Unions:-
Structure data type is so defined that
it is able to accept heterogeneous data type. Thus structure data type allows
to pack together different types of data values as a single unit. Unions are
also similar to structure data type except that the members are over laid one on
top of another, so members of a union data type share the same memory.
Unions obey the same syntactic rules
as structures. We can access elements with either the dot operator (.) or right
arrow operator (à). The major use of union in memory
space utilization. Union would hold a value for one data type of the larger
storage of their members.
Ex-
#include<stdio.h>
#include<conio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
union value
{
int i;
float f;
struct date
bdate;
};
union value x;
x.i=10;
x.f=-1456.45;
x.bdate.day=25;
x.bdate.month=7;
x.bdate.year=1972;
printf(“frist
member%d\n”,x.i);
printf(“second
member%f\n”,x.f);
printf(“structure:\n”);
printf(“%d%d%d\n”,x.bdate.day,x.bdate.month,x.bdate.year);
}
No comments:
Post a Comment