Back to: C Tutorials For Beginners and Professionals
Difference Between Structure and Union in C
In this article, I will discuss the Difference Between Structure and Union in C Language with Examples. Please read our previous article, discussing Union in C Language with Examples.
Difference Between Structure and Union in C Language
In C programming, structures (struct) and unions (union) are user-defined data types that allow storing different data types together. However, they differ significantly in how they manage memory and how they’re typically used.
Structure (struct) in C Language
In a struct, each member has its own memory space. The total memory size of a struct is the sum of the sizes of all its members, often with additional padding for alignment purposes. All members of a structure can be stored and accessed simultaneously. Each member has a unique memory location.
Structs are used when you want to group different data types to form a complex data type, and you need to use all the data simultaneously. If you need to keep track of a student’s information, including name, ID, and GPA, all these pieces of information are required at the same time, making a structure the appropriate choice.
Example of Structure in C:
#include <stdio.h> struct Student { char name[50]; int age; float marks; }; int main() { struct Student student1; strcpy(student1.name, "Alice"); student1.age = 20; student1.marks = 72.5; printf("Name: %s\nAge: %d\nMarks: %.2f\n", student1.name, student1.age, student1.marks); return 0; }
In this example, student1 is a structure that holds a student’s name, age, and marks. Each field is stored separately in memory.
Union (union) in C Language
In a union, all members share the same memory space. The size of its largest member determines the size of a union. At any given time, a union can store only one of its members. Storing a new member value overwrites the previous member’s data.
Unions are useful when you need to work with only one type of data at a time, but the type of data can change. They are memory-efficient for such scenarios. Unions are commonly used in situations like interpreting the raw data from a sensor where the data can be an integer, a float, or a byte array, but only one type is needed at a time.
Example of Union in C:
#include <stdio.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; // At this point, the integer value is overwritten printf("data.f: %.2f\n", data.f); return 0; }
In this example, the union data can store an int, a float, or a char array, but only one at a time. When data.f is assigned a value, it overwrites the memory of data.i.
Key Differences Between Struct and Union in C Language
Memory Allocation:
- Structure: Each member within a structure is allocated its own memory location. The size of the structure is the sum of the sizes of all its members plus any padding added by the compiler for alignment purposes.
- Union: All members of a union share the same memory location. The size of its largest member determines the size of a union.
Memory Usage:
- Structure: As each member has its own memory space, multiple members of a structure can be accessed and modified independently at the same time.
- Union: Since all members share the same memory space, changing the value of one member will affect the value of others. At any point, only one member contains meaningful data.
Purpose:
- Structure: Structures are used when you want to group different data types together, and each element is expected to be used simultaneously.
- Union: Unions are useful when you want to use the same memory space for different types of data, but not at the same time. They are often used in memory and resource-constrained environments or when manipulating different types of data in a single memory location, such as hardware registers.
Typical Use Cases:
- Structure: Commonly used to create complex data types that can hold multiple related data items. For instance, representing a student with name, age, and grade.
- Union: Commonly used in scenarios like type conversion without a cast or when interfacing with hardware where certain registers can hold multiple types of data.
Choosing Between Struct and Union in C
- Use a struct when you need to store multiple pieces of information about an entity, and all or most of the information needs to be accessed simultaneously.
- Use a union when you need to store different types of data in the same memory location, but you’ll only be using one type at a time.
In the next article, I will discuss the Union Real-Time Examples in C Language. In this article, I explain the Difference Between Structure and Union in C Language with Examples. I hope you enjoy this Difference Between Structure and Union in C Language article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.