C Unions
What are Unions in C?
A union in C is a user-defined data type that allows different data types to share the same memory location.
Unlike structures, where each member has its own memory, all members of a union use the same memory space. As a result, only one member can hold a valid value at a time.
Unions are useful when memory optimization is important, especially in embedded systems and low-memory applications.
Example
union Data
{
int id;
float marks;
char grade;
};
Explanation
• Data is the name of the union.
• id, marks, and grade share the same memory location.
• Only one member can store a valid value at a time.
• Unions help reduce memory usage.
• They are commonly used in memory-efficient applications.
Why Unions are Important?
Unions are important because they:
• Save memory by allowing multiple members to share the same memory location.
• Improve memory efficiency in programs.
• Are useful in embedded systems and hardware programming.
• Help optimize applications with limited memory.
• Provide an efficient way to manage different data types.
Types of Unions in C
Unions can be used in different ways depending on the application.
The most common types are:
• Basic Union
• Union with Different Data Types
• Anonymous Union
Each type is useful for specific programming requirements.
Basic Union
A basic union contains multiple members that share the same memory location.
Only one member should be used at a time because updating one member changes the value stored by the others.
Example
union Data
{
int number;
float marks;
};
union Data d;
Explanation
• Data is a union.
• number and marks share the same memory location.
• d is a union variable.
• Only one member should store a value at a time.
Union with Different Data Types
A union can contain members of different data types such as integers, floating-point numbers, and characters.
Since all members share the same memory location, only one member should be used at a time.
Example
union Data
{
int id;
float salary;
char grade;
};
Explanation
• id stores an integer value.
• salary stores a floating-point value.
• grade stores a single character.
• All members share the same memory location.
• Writing a new value to one member overwrites the previous value.
Anonymous Union
An anonymous union is a union without a name.
Its members can be accessed directly without using a union variable name.
Anonymous unions are supported by modern C compilers and are useful in special programming situations.
Example
union
{
int id;
float marks;
};
Explanation
• The union has no name.
• Members can be accessed directly in supported environments.
• Anonymous unions reduce unnecessary naming.
• They are mainly used in advanced C programming.
Advantages of Unions
• Reduce memory usage by sharing the same memory location.
• Improve memory efficiency.
• Useful in embedded systems and hardware programming.
• Help optimize applications with limited memory.
• Allow different data types to use the same memory space efficiently.
Example Program
Program
#include <stdio.h>
union Data
{
int id;
float marks;
char grade;
};
int main()
{
union Data d;
d.id = 101;
printf(“ID: %d\n”, d.id);
d.marks = 89.5;
printf(“Marks: %.1f\n”, d.marks);
d.grade = ‘A’;
printf(“Grade: %c\n”, d.grade);
return 0;
}
Output
ID: 101
Marks: 89.5
Grade: A
Explanation
• Data is a user-defined union.
• d is a union variable.
• id, marks, and grade share the same memory location.
• Assigning a new value replaces the previous member’s value.
• Unions help save memory because all members share the same storage.
Key Points
• A union is a user-defined data type.
• All members of a union share the same memory location.
• Only one member should contain a valid value at a time.
• Unions are used to optimize memory usage.
• Unions are useful in embedded systems and hardware programming.
• Different data types can share the same memory space.
• Unions are different from structures because structures allocate separate memory for each member.
