Introduction to Union

Preview
You must first complete Introduction to Structure in C before viewing this Lesson

Introduction to Union

It is similar to a structure but processing and storage is different. Union can also contain different types of elements having different data types.

The main difference between a structure and a union is that all the members of a structure have different memory locations whereas all members of a union share common storage space.

When a union is defined, compiler automatically allocates memory to hold element of union having largest size in term of bytes.

The very main advantage of a union is that it saves memory of computer as each member of union is not allocated separate memory space.

Syntax of Union

union <tag_name>

{

<type1> member1;

<type2> member2;

:

:

<typeN> memberN;

};

Here, union is the keyword which specifies that a union is being created.

tag_name referes to the name of union . The name of union should follow all the rules of a legal identifier in C language.

{ Opening brace specifies that union has started.

type1, type2,… typeN refer to data types of various elements of a union. The data type may be predefined  data types or user defined data types of C language.

member1, member2,… memberN refer to elements declared within the union. They are also known as members of a union.

} Closing brace specifies that structure has ended.

; Semicolon is necessary to write after closing brace.



Declaration of a Union variable

We can declare variables of a union just as we declare variables of other data types. For declaring a variable we need to write the union keyword followed by the tag name of union which is further followed by names of union type variables.

In C language, syntax for declaring a structure  variable is:

union <tag_name> var1,var2,…..varN;

Here, tag_name is the name of union of which we want to declare variables.

var1,var2,……..varN are names of union variables.

It should be noted that the members of a union are not variables themselves. They don’t occupy any memory until some variable of union is declared. Union definition always starts with an opening brace and ends with a closing brace followed by a semicolon.



Program to demonstrate the use of a union.
#include<string.h>
#include<stdio.h>
union data
{
int a;
float b;
float c;
};
int main()
{
union data d;
printf(“%d”,sizeof(d));
d.a=13;
d.b=5.5;
d.c=4.6;
printf(“\nd.a=%d”,d.a);
printf(“\nd.b=%f”,d.b);
printf(“\nd.c=%f”,d.c);
return(0);
}
Output
Size of union variable d=4
d.a=13137
d.b=4.600000
d.c=4.600000
Description
In the above program, size of union variable d is displayed as 4 because the size of largest element of union is 4 (float). The value of d.c is displayed correctly as the union variable can hold value of only one of its elements at a time. The latest value stored is of d.c.



 Difference between  structure and union

StructureUnion
1. Structure is defined as a collection of heterogeneous types of elements.1.  Union is defined as a collection of heterogeneous types of elements.
2. We need to use the keyword struct to define a structure.2. We need to use the keyword union to define a structure.
3. We can access all the members of structure at anytime.3. We can access only one member of union at a time.
4. Memory is allocated for all variables.4. Allocates memory for that variable which has the maximum size.
5. All members of structure can be initialized.5.  Only first element of a union can be initialized.
6. More memory used while using a strucuture.6. Less memory is used while using a union.
7. Structure stores its members at unique memory locations.7. Union stores its members in the same place

8. Syntax for declaring a structure is:

struct <tag_name>

{

<type1> member1;

<type2> member2;

:

:

<typeN> memberN;

};

8. Syntax for declaring a union is:

union <tag_name>

{

<type1> member1;

<type2> member2;

:

:

<typeN> memberN;

};



Best Books of C





Lesson tags: declare union variable in c, difference between structure and union, program to use union in c, size of a union variable in c, union in c
Back to: C Programming Language