A union in C is a user-defined data type that allows different members to share the same memory location. A union can contain members of different data types, but only one member's value should be considered active at a time.
A union is a user-defined data type in C where all members share the same memory location.
union Data
{
int number;
float price;
char grade;
};
The union can contain members of different types, but they use the same storage area.
Unions are useful when different types of information need to use the same memory area at different times.
The general syntax for defining a union is:
union UnionName
{
data_type member1;
data_type member2;
data_type member3;
};
The semicolon after the closing brace is required.
union Data
{
int number;
float price;
char grade;
};
This defines a union type named union Data.
It does not automatically create a variable named data.
After defining the union, a variable can be declared using the union type.
union Data data1;
The variable data1 can use any one of its members at a time.
The dot operator . is used to access members of a union variable.
data1.number = 100;
printf("%d", data1.number);
The syntax is similar to accessing structure members.
All members share the same storage. If you assign a new value through one member, the previously stored representation should no longer be treated as the active value of the union.
union Data data;
data.number = 100;
printf("%d", data.number);
data.price = 25.5f;
printf("%.2f", data.price);
The second assignment uses the same storage for price.
The members of a union share storage. Therefore, the size of a union is sufficient to hold its largest member, subject to implementation-defined alignment and padding.
union Data
{
int number;
double value;
char name[30];
};
The union must provide enough storage for its largest member.
The sizeof operator can be used to determine the size of a union.
union Data
{
int number;
double value;
char name[30];
};
printf("%zu", sizeof(union Data));
The result is implementation-dependent but must be large enough to hold any member.
#include <stdio.h>
union Data
{
int number;
float price;
};
int main()
{
union Data data;
data.number = 100;
printf("Number = %d", data.number);
return 0;
}
#include <stdio.h>
union Data
{
int number;
float price;
};
int main()
{
union Data data;
data.price = 99.50f;
printf("Price = %.2f", data.price);
return 0;
}
union Data
{
int number;
float price;
char grade;
};
union Data data;
data.grade = 'A';
printf("%c", data.grade);
The character member uses the same shared storage as the other members.
A union can be initialized when it is declared.
union Data
{
int number;
float price;
};
union Data data = {100};
The first member is initialized by this form.
A designated initializer can specify which member is initialized.
union Data
{
int number;
float price;
};
union Data data = {
.price = 99.50f
};
This initializes the price member.
| Structure | Union |
|---|---|
| Members have separate storage | Members share storage |
| Multiple members can hold values at the same time | One member's stored value is normally treated as active at a time |
| Size generally accounts for all members and padding | Size is based primarily on the largest member and alignment |
| Useful for records | Useful for alternative representations |
A union can contain a structure as one of its members.
struct Student
{
int id;
char name[30];
};
union Data
{
int number;
struct Student student;
};
The union can use either the integer member or the structure member.
A structure can contain a union as a member.
union Contact
{
int phone;
char email[50];
};
struct Customer
{
int id;
union Contact contact;
};
This can represent alternative types of contact information.
struct Customer customer;
customer.id = 101;
customer.contact.phone = 987654321;
The union member can be accessed using the dot operator through the structure.
A pointer can store the address of a union variable.
union Data data;
union Data *ptr = &data;
ptr->number = 500;
The arrow operator is used to access a union member through a pointer.
A union can be passed to a function by value.
void display(union Data data)
{
printf("%d", data.number);
}
The function receives a copy of the union object.
A pointer to a union can be passed to a function.
void display(union Data *data)
{
printf("%d", data->number);
}
Passing a pointer avoids copying the union and allows the function to modify the union when appropriate.
The typedef keyword can create a shorter name for a union type.
typedef union
{
int number;
float price;
char grade;
} Data;
Now we can declare a variable using:
Data data1;
A union can contain an array as a member.
union Data
{
int number;
char text[30];
};
union Data data;
data.text[0] = 'H';
data.text[1] = 'i';
data.text[2] = '\0';
printf("%s", data.text);
The array uses the union's shared storage.
Because union members share memory, programs sometimes access the stored representation through a different member. However, interpreting an object through a different union member has rules and implementation-defined or unspecified aspects depending on the situation and C standard version.
For beginner programs, use a union by writing and then reading the member that represents the current stored value.
Union members share the same storage, so assigning one member changes the shared memory used by the union.
union Data data;
data.number = 100;
data.price = 50.5f;
Do not expect both assignments to represent two independent stored values as they would in a structure.
Use the dot operator with a union variable and the arrow operator with a pointer to a union.
union Data data;
union Data *ptr = &data;
data.number = 100;
ptr->number = 200;
Using the correct operator makes member access clear and valid.
#include <stdio.h>
union Data
{
int number;
float price;
char grade;
};
int main()
{
union Data data;
data.number = 100;
printf("Number = %d\n", data.number);
data.price = 75.50f;
printf("Price = %.2f\n", data.price);
data.grade = 'A';
printf("Grade = %c\n", data.grade);
printf("Union Size = %zu\n", sizeof(data));
return 0;
}
This program demonstrates that different members use the same union storage.
#include <stdio.h>
enum Type
{
TYPE_INT,
TYPE_FLOAT
};
union Value
{
int number;
float decimal;
};
struct Data
{
enum Type type;
union Value value;
};
int main()
{
struct Data data;
data.type = TYPE_INT;
data.value.number = 100;
if(data.type == TYPE_INT)
{
printf("Value = %d", data.value.number);
}
return 0;
}
A tag such as an enum can be used to record which union member currently represents the stored value.
Unions can be useful in:
Create a C program that:
EmployeeData.Observe how all union members share the same memory.
. accesses a member through a union variable.-> accesses a member through a union pointer.typedef can provide a shorter name for a union type.Question: How do the members of a union use memory?