Share this post on:

Union is a derived data type structure and it can also contain member of different data types. The syntax used for declaration of a union type, declaration of a union variable and for accessing member is similar to that used in structure, but here keyword union is used instead of struct. The main difference between union and structure is in the way memory is allocated for the member. In a structure each member has its own memory location, whereas members of union share the same memory location. When a variable of type union is declared, compiler allocates sufficient memory to hold the largest member in the union. Since all members share the same memory location, we can use only one member at a time. Thus union is used at a time.

The syntax of declaration of a union is –

union union_name

{

data_type member1;

data_type member2;

};

Like structure variable , union variables can be declared along with the declaration or separately. for example –

union

{

data_type member1;

data_type member2;

}

variable_name;

union union_name variable_name;

we can access the union member using the same syntax used for structure. If we have a union variable then the members can be accessed using dot(.) operator, and if e have a pointer to union then the members can be accessed using the arrow (->) operator.

Example : Program for accessing union member?

#include<stdio.h>

int main (void)

{

union result

{

int marks;

char grade;

float per;

}

res;

res.marks=90;

printf(“Marks : %d\n”,res.marks);

res.grade=’A’;

printf(“Grade : %c\n, res.grade);

res.per =85.6;

printf(“Percentage : %f\n,res.per);

}

Output:

Marks : 90

Grade : A

Percentage : 85.500000

Before the first printf, value 90 is assigned to the union member marks, after first printf, the value ‘A’ is assigned to the union member grade. Only one member of union can hold value at a time, all members can’t be used simultaneously. Thus a union variable of type result can be treated as either an int variable or char variable or a float variable. It is the responsibility of the programmer to keep track of member that currently holds the value, If the programmer tries to extract the value of any other member then the result is implemented defined.

Union variable can also be initialized, but there is a limitation, but there is a limitation. we know that due to sharing of memory, all the member can’t hold values simultaneously. so during initialization also only member one member can be given an initial value, and this privilege is given to the first member. Hence only the first member of a union can be given an initial value. the type of the initialization should match with the type of the first member.

For example we can initialization the above union variable as-

union result res = {78};

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

Your email address will not be published. Required fields are marked *