Difference between structure and union in C Programming

In C programming, both structures (structs) and unions are used to define custom data types that can hold multiple variables of different data types. However, they have distinct differences in terms of how they allocate memory and how they store data:

1. Memory Allocation:

  • Structure (struct): In a struct, each member (or field) is allocated its own separate memory location. This means that the memory required for each member is allocated individually, and the total memory occupied by the struct is the sum of the memory used by its members. This allows a struct to hold multiple variables of different types but can result in larger memory consumption.
  • Union: In a union, all members share the same memory location. A union only occupies enough memory to store its largest member. While unions can hold variables of different types, they can only hold one value at a time, and accessing a different member will overwrite the previous one. Unions are often used when you want to save memory by sharing the same memory location for different types of data.

2. Memory Usage:

  • Structure (struct): Since each member in a struct has its own memory location, a struct’s memory usage is the sum of the memory sizes of its members. This can result in larger memory consumption, especially if the structure contains multiple members of different types.
  • Union: A union’s memory usage is determined by the size of its largest member. Since all members share the same memory location, a union will occupy enough memory to accommodate the largest member, which can be more memory-efficient in certain situations.

3. Accessing Data:

  • Structure (struct): You can access each member of a struct independently. This allows you to store and retrieve multiple pieces of data simultaneously.
  • Union: In a union, you can only access one member at a time. Writing to one member will overwrite the data in the union, so you must be careful when using unions to avoid unexpected behavior.

4. Use Cases:

  • Structure (struct): Structs are typically used when you need to group related data with different types into a single composite data type. They are suitable for situations where you want to access and manipulate multiple pieces of data together.
  • Union: Unions are used when you need to save memory and when you only need to store one of several possible data types at any given time. They are useful for situations where you have mutually exclusive data options.

AspectStructure (struct)Union
Memory AllocationEach member has its own separate memory location.All members share the same memory location.
Memory UsageTotal memory usage is the sum of the memory used by members.Memory usage is determined by the size of the largest member.
Accessing DataYou can access each member independently.You can only access one member at a time.
Use CasesSuitable for grouping related data with different types.Used when you need to save memory and store mutually exclusive data.
Examplec struct Person { char name[50]; int age; };c union Data { int num; float value; };
Difference between Structure and Union

union occupies less memory than structure

union variable can store one value at a time & same memory location, and can be used to store multiple types of data.

Use GCC compiler for below

Example of Structure and Union

 struct Person {
char name[50];
int age;
};union Data {
int num;
float value;
};int main() {
struct Person person;
union Data data;// In a struct, you can access both name and age independently.
strcpy(person.name, "John");
person.age = 30;// In a union, you can only store and access one member at a time.
data.num = 42;
printf("Value: %d\n", data.num);
data.value = 3.14;
printf("Value: %f\n", data.value);return 0;
}