x

C – Union

Prev     Next

C Union is also like structure, i.e. collection of different data types which are grouped together. Each element in a union is called member.


  • Union and structure in C  are same in concepts, except allocating memory for their members.
  • Structure allocates storage space for all its members separately.
  • Whereas, Union allocates one common storage space for all its members
  • We can access only one member of union at a time. We can’t access all member values at the same time in union. But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Wheras Structure allocates storage space for all its members separately.
  • Many union variables can be created in a program and memory will be allocated for each union variable separately.
  • Below table will help you how to form a C union, declare a union, initializing and accessing the members of the union.
Using normal variable Using pointer variable
Syntax:
union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Syntax:
union tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};
Example:
union student
{
int  mark;
char name[10];
float average;
};
Example:
union student
{
int  mark;
char name[10];
float average;
};
Declaring union using normal variable:
union student report;
Declaring union using pointer variable:
union student *report, rep;
Initializing union using normal variable:
union student report = {100, “Mani”, 99.5};
Initializing union using pointer variable:
union student rep = {100, “Mani”, 99.5};
report = &rep;
Accessing union members using normal variable:
report.mark;
report.name;
report.average;
Accessing union members using pointer variable:
report  -> mark;
report -> name;
report -> average;

Example program for C union:

Output:

Union record1 values example
Name :
Subject :
Percentage : 86.500000;
Union record2 values example
Name : Mani
Subject : Physics
Percentage : 99.500000

Explanation for above C union program:

There are 2 union variables declared in this program to understand the difference in accessing values of union members.

Record1 union variable:

  • “Raju” is assigned to union member “record1.name” . The memory location name is “record1.name” and the value stored in this location is “Raju”.
  • Then, “Maths” is assigned to union member “record1.subject”. Now, memory location name is changed to “record1.subject” with the value “Maths” (Union can hold only one member at a time).
  • Then, “86.50” is assigned to union member “record1.percentage”. Now, memory location name is changed to “record1.percentage” with value “86.50”.
  • Like this, name and value of union member is replaced every time on the common storage space.
  • So, we can always access only one union member for which value is assigned at last. We can’t access other member values.
  • So, only “record1.percentage” value is displayed in output. “record1.name” and “record1.percentage” are empty.

Record2 union variable:


  • If we want to access all member values using union, we have to access the member before assigning values to other members as shown in record2 union variable in this program.
  • Each union members are accessed in record2 example immediately after assigning values to them.
  • If we don’t access them before assigning values to other member, member name and value will be over written by other member as all members are using same memory.
  • We can’t access all members in union at same time but structure can do that.

Example program – Another way of declaring C union:

In this program, union variable “record” is declared while declaring union itself as shown in the below program.

Output:

Name :
Subject :
Percentage : 86.500000

Note:

  • We can access only one member of union at a time. We can’t access all member values at the same time in union.
  • But, structure can access all member values at the same time. This is because, Union allocates one common storage space for all its members. Where as Structure allocates storage space for all its members separately.

Difference between structure and union in C:

C Structure
C Union

Structure allocates storage space for all its members separately.

Union allocates one common storage space for all its members.
Union finds that which of its member needs high storage space over other members and allocates that much space
Structure occupies higher memory space. Union occupies lower memory space over structure.
We can access all members of structure at a time. We can access only one member of union at a time.
Structure example:
struct student
{
int mark;
char name[6];
double average;
};
Union example:
union student
{
int mark;
char name[6];
double average;
};
For above structure, memory allocation will be like below.
int mark – 2B
char name[6] – 6B
double average – 8B 
Total memory allocation = 2+6+8 = 16 Bytes
For above union, only 8 bytes of memory will be allocated since double data type will occupy maximum space of memory over other data types. 
Total memory allocation = 8 Bytes

Prev     Next



Like it? Please Spread the word!