Introduction
In the previous chapters, we explored various aspects of C programming, including structures. In this chapter, we will learn about Unions in C programming with examples.
What is a Union?
A union is a user-defined data type that allows you to store different types of data in the same memory location. Unlike structures, where each member has its own storage, all members of a union share the same memory location. This means that at any given time, a union can store only one of its members.
Syntax
The basic syntax for defining a union in C is as follows:
union union_name {
data_type member1;
data_type member2;
...
data_type memberN;
};
union
: Keyword used to define a union.union_name
: Name of the union.data_type
: Data type of the union members.member1
,member2
, …,memberN
: Names of the union members.
Example: Defining and Using a Union
Let’s look at a simple example to understand how to define and use a union.
Example: Defining and Using a Data Union
#include <stdio.h>
// Defining the union
union Data {
int i;
float f;
char str[20];
};
int main() {
// Declaring a union variable
union Data data;
// Accessing and modifying union members
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.2f\n", data.f);
strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str);
return 0; // Returning 0 to indicate successful execution
}
Output:
data.i: 10
data.f: 220.50
data.str: Hello
In this example, we defined a union named Data
with three members: i
, f
, and str
. We then declared a variable of type Data
, initialized its members one by one, and printed their values. Note that at any given time, only one member of the union can hold a value.
Memory Allocation in Unions
The size of a union is determined by the size of its largest member. This is because all members of the union share the same memory location.
Example: Memory Allocation in Unions
#include <stdio.h>
// Defining the union
union Data {
int i;
float f;
char str[20];
};
int main() {
// Declaring a union variable
union Data data;
// Printing the size of the union
printf("Size of union: %lu bytes\n", sizeof(data));
return 0; // Returning 0 to indicate successful execution
}
Output:
Size of union: 20 bytes
In this example, the size of the union Data
is determined by its largest member, which is str
(20 bytes).
Accessing Union Members
Union members are accessed using the dot operator (.
). The syntax for accessing a union member is as follows:
union_variable.member
Example: Accessing Union Members
#include <stdio.h>
#include <string.h>
// Defining the union
union Data {
int i;
float f;
char str[20];
};
int main() {
// Declaring a union variable
union Data data;
// Accessing and modifying union members
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.2f\n", data.f);
strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str);
return 0; // Returning 0 to indicate successful execution
}
Output:
data.i: 10
data.f: 220.50
data.str: Hello
Union vs. Structure
While both unions and structures are used to group different types of variables, they have some key differences:
-
Memory Allocation:
- In structures, each member has its own memory location.
- In unions, all members share the same memory location.
-
Size:
- The size of a structure is the sum of the sizes of its members.
- The size of a union is determined by its largest member.
-
Usage:
- Use structures when you need to store multiple variables of different types simultaneously.
- Use unions when you need to store different types of variables, but only one at a time, to save memory.
Example: Union vs. Structure
#include <stdio.h>
#include <string.h>
// Defining the structure
struct Student {
int id;
float grade;
char name[20];
};
// Defining the union
union Data {
int i;
float f;
char str[20];
};
int main() {
// Declaring structure and union variables
struct Student student = {1, 85.5, "Alice"};
union Data data;
// Printing the size of the structure and union
printf("Size of structure: %lu bytes\n", sizeof(student));
printf("Size of union: %lu bytes\n", sizeof(data));
// Accessing structure members
printf("Student ID: %d\n", student.id);
printf("Student Grade: %.2f\n", student.grade);
printf("Student Name: %s\n", student.name);
// Accessing union members
data.i = 10;
printf("data.i: %d\n", data.i);
data.f = 220.5;
printf("data.f: %.2f\n", data.f);
strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str);
return 0; // Returning 0 to indicate successful execution
}
Output:
Size of structure: 28 bytes
Size of union: 20 bytes
Student ID: 1
Student Grade: 85.50
Student Name: Alice
data.i: 10
data.f: 220.50
data.str: Hello
In this example, the structure Student
has a size of 28 bytes (sum of sizes of all members), while the union Data
has a size of 20 bytes (size of the largest member).
Conclusion
Unions in C provide a way to store different types of data in the same memory location, saving memory by sharing the same space. They are useful when you need to store multiple types of data but only one type at a time. By understanding how to define, use, and manipulate unions, you can efficiently manage memory in your C programs. Knowing when to use unions versus structures is essential for writing efficient and effective C code.