Unions

A union is a derived data type—like a structure—with members that share the same storage space.
For different situations in a program, some variables may not be relevant, but other variables are—so a union shares the space instead of wasting storage on variables that are not being used.
The members of a union can be of any data type.
The number of bytes used to store a union must be at least enough to hold the largest member.
Operations on Unions (same as Structures)
The operations that can be performed on a union are:
assigning a union to another union of the same type,
taking the address (&) of a union variable,
and accessing union members using the structure member operator and the structure pointer operator.
Unions may not be compared using operators == and != for the same reasons that structures cannot be compared.
Program 1:
#include <stdio.h>
union abc
{
int a;
char b;
}var={66};
int main()
{
printf("\n var.a = %d", var.a);
printf("\n var.b = %c", var.b);
printf("\n address of var = %p", &var);
printf("\n address of var.a = %p", &(var.a));
printf("\n address of var.b = %p", &(var.b));
printf("\n bytes allocated for var = %d", sizeof(var));
return 0;
}
Output:

Explanation:
Here we initialized the integer member var.a with 66. Since both the members share the same memory, the character member var.b also contains 66 which is interpreted as an ASCII value and hence the character 'B' in the output.
Program 2:
#include <stdio.h>
union abc
{
int a;
float b;
}var={66};
int main()
{
printf("\n var.a = %d", var.a);
printf("\n var.b = %f", var.b);
printf("\n address of var = %p", &var);
printf("\n address of var.a = %p", &(var.a));
printf("\n address of var.b = %p", &(var.b));
printf("\n bytes allocated for var = %d", sizeof(var));
return 0;
}
Output:

The bit pattern of integer 66 assigned to var.a in the shared memory cannot be interpreted as a floating point value, because it doesn't follow the IEEE 754 format.
Program 3:
#include <stdio.h>
union abc
{
float b;
int a;
}var={5.4};
int main()
{
printf("\n var.a = %d", var.a);
printf("\n var.b = %f", var.b);
printf("\n address of var = %p", &var);
printf("\n address of var.a = %p", &(var.a));
printf("\n address of var.b = %p", &(var.b));
printf("\n bytes allocated for var = %d", sizeof(var));
return 0;
}
Output:

Here we are assigning 5.4 to var.b.
Now 5.4 is converted to its binary form using the IEEE 754 format.
Observe the binary representation:

Now when we try to print var.a, which is an integer type, the binary number stored in this same memory location will be interpreted as an integer:

Program 4: Initializing a union variable initializes the first member inside the union by default.
#include <stdio.h>
union abc
{
int a;
float b;
}var={5.4};
int main()
{
printf("\n var.a = %d", var.a);
printf("\n var.b = %f", var.b);
printf("\n address of var = %p", &var);
printf("\n address of var.a = %p", &(var.a));
printf("\n address of var.b = %p", &(var.b));
printf("\n bytes allocated for var = %d", sizeof(var));
return 0;
}
Output:

Program 5:

Error:

Program 6: Pointers store addresses and addresses are integers.
So technically you can use integer values to initialize/assign to pointer variable of any type
#include <stdio.h>
union abc
{
char * a;
float b;
}var={66};
int main()
{
printf("\n var.a = %d", var.a);
printf("\n var.b = %f", var.b);
printf("\n address of var = %p", &var);
printf("\n address of var.a = %p", &(var.a));
printf("\n address of var.b = %p", &(var.b));
printf("\n bytes allocated for var = %d", sizeof(var));
return 0;
}
Warning:

Output:


