UNION

Like structures, union is a collection of data items of different types. The only difference between structure and union is from the view point of storage of their members. All the members of a structure are assigned separate memory locations, but union allows you to view a single storage area in more then one way, i.e all the members of a union the same memory area.

Therefore, union are used to converse memory, and memory requirement is that much as is required to access the largest number of union, whereas, in structure, the reservation of memory is the sum total of memories to be used by all the members of a structure.

DEFINING A UNION....

A Union can be defined in the same way as structure are defined, but unions use keyword union a t the time of definition instead of struct.

THE SYNTAX IS....

Storage class Union tag_name

{

Data_type1 member1;

` Data_type2 member2;

Data_type n member n;

}

Here storage class is optional, but keyword Union is compulsory to be mentioned.

Example....

Union exam

{

int x;

float y;

double z;

}

Variable of union type can be declared at the time of definition

Union exam

{

int z;

float y;

double z;

}a;

Variable of union type can be declared by the following declaration statement.

Union exam a ;

Union--------keyword union

Exam--------tag name

a--------------variable of union type

;--------------end of the statement(semicolon)

MEMORY ALLOCATION TO MEMBER OF UNION:

When you declare a variable the memory assigned to it is the memory required to store the largest member of union.

SIZE OF UNION....

#include<stdio.h>

#include<conio.h>

void main()

{

union exam

{

int x;

float y;

double z;

}e;

clrscr();

printf("memory occupied by variable e is %d bytes",sizeof(e));

getch();

}

Output...

Memory occupied by variable e is 8 bytes.

INITIALIZATION OF MEMBERS OF UNION:

As you can initialize the members of the structure at the time of declaration, in the same way, you can also initialize the members of union but with a difference. The members of union share the same memory, so all the members can not be initialized at the same time.

The following code is invalid and gives the compilation error when executed.

union exam

{

int x;

float y;

double z;

};

Union exam a={7,9.0,4791.53}; -----------Invalid

Whenever you want to initialize union variable , always initialize its first member only.

union exam

{

int x;

float y;

double z;

};

Union exam a={7};-----------Valid

Example(Initialization of Union)

#include<stdio.h>

#include<conio.h>

void main()

{

union exam

{

int x;

float y;

double z;

}e={1};

printf("Value of x after initialization is %d\n",e.x);

e.y=7;

printf("value of y %f \n",e.y);

printf("value of x after assigning value to y is %d \n",e.x);

}

Output.....

Value of x after initialization is 1

Value of y 7.000000

Value of x after assigning value to y is 0;

Member of union can be accessed in the same way as the members of structure are accessed i.e., by using the dot operator.