Unions
Unions
UNIONS
Definition -
A union is a special data type available in C that allows to store different data types in the same memory
location. You can define a union with many members, but only one member can contain a value at any
given time. Unions provide an efficient way of using the same memory location for multiple-purpose.
Syntax for Defining a Union:
✓ The union statement defines a new data type with more than one member for your program.
union union_name
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
✓ The union name (tag) is optional and each member definition is a normal variable definition, such as
int i; or float f; or any other valid variable definition. At the end of the union's definition, before the
final semicolon, you can specify one or more union variables but it is optional.
Example:
union Data
{
int i;
float f;
char str[20];
} d;
✓ Now, a variable of ‘d’ type can store an integer, a floating-point number, or a string of characters. It
means a single variable, i.e., same memory location, can be used to store multiple types of data. You
can use any built-in or user defined data types inside a union based on your requirement.
✓ The memory occupied by a union will be large enough to hold the largest member of the union. For
example, in the above example, Data type will occupy 20 bytes of memory space because this is the
maximum space which can be occupied by a character string.
Output:
Explanation:
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: To access one union member at a time.
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
Here, all the members are getting printed very well because one member is being used at a time.
Structure Union
1.The keyword struct is used to define a 1. The keyword union is used to define a
structure union.
2. When a variable is associated with a 2. When a variable is associated with a union,
structure, the compiler allocates the memory the compiler allocates the memory by
for each member. The size of structure is considering the size of the largest memory.
greater than or equal to the sum of sizes of its So, size of union is equal to the size of largest
members. The smaller members may end with member.
unused slack bytes.
3. Each member within a structure is assigned 3. Memory allocated is shared by individual
unique storage area of location. members of union.
4. The address of each member will be in 4. The address is same for all the members of
ascending order This indicates that memory a union. This indicates that every member
for each member will start at different offset begins at the same offset value.
values.
5 Altering the value of a member will not 5. Altering the value of any of the member
affect other members of the structure. will alter other member values.
6. Individual Structure member can be 6. Only one Union member can be accessed at
accessed at a time a time.
7. Several members of a structure can 7. Only one member of a union can be
initialize at once. initialized.
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
}uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
}sJob;
int main()
{
printf("size of union = %d", sizeof(uJob)); printf("\nsize of
structure = %d", sizeof(sJob)); return 0;
}
Output:
size of union = 32
size of structure = 40
✓ But, the memory required to store a union variable is the memory required for the largest element of
a union.
#include<stdio.h>
struct number1
{
int i;
float f;
char c;
}sn;
union number2
{
int i;
float f;
char c;
}un;
void main()
{
printf("Details of structure\n");
printf("size of structure number1=%d\n",sizeof(sn));
sn.i=10;
sn.f=89.00;
sn.c='X';
printf("sn.i=%d\n sn.f=%f\n sn.c=%c\n",sn.i,sn.f,sn.c);
printf("Details of Union\n");
printf("size of Union number2=%d\n",sizeof(un));
un.i=10;
un.f=89.00;
un.c='X';
printf("un.i=%d\n un.f=%f\n un.c=%c\n",un.i,un.f,un.c);
}
Output:
size of structure number1= 7
sn.i =10
sn.f =89.00
sn.c =X
size of Union number2 = 4
un.i =Garbage value
un.f = Garbage value
un.c =X