Difference Between Structure and Union in C
Last Updated :
27 Dec, 2024
In C programming, both structures and unions are used to group different types of data under a single name, but they behave in different ways. The main difference lies in how they store data.
The below table lists the primary differences between the C structures and unions:
Parameter | Structure | Union |
---|
Definition | A structure is a user-defined data type that groups different data types into a single entity. | A union is a user-defined data type that allows storing different data types at the same memory location. |
---|
Keyword | The keyword struct is used to define a structure | The keyword union is used to define a union |
---|
Size
| The size is the sum of the sizes of all members, with padding if necessary. | The size is equal to the size of the largest member, with possible padding. |
---|
Memory Allocation | Each member within a structure is allocated unique storage area of location. | Memory allocated is shared by individual members of union. |
---|
Data Overlap | No data overlap as members are independent. | Full data overlap as members shares the same memory. |
---|
Accessing Members | Individual member can be accessed at a time. | Only one member can be accessed at a time. |
---|
Structures
A structure in C is a collection of variables, possibly of different types, under a single name. Each member of the structure is allocated its own memory space, and the size of the structure is the sum of the sizes of all its members.
Syntax
struct name {
member1 definition;
member2 definition;
...
memberN definition;
};
Example:
C
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
// Create a structure variable
struct Student s1 = {"Geek", 20, 85.5};
// Access structure members
printf("%s\n", s1.name);
printf("%d\n", s1.age);
printf("%.2f\n", s1.grade);
printf("Size: %d bytes", sizeof(s1));
return 0;
}
OutputGeek
20
85.50
Size: 60 bytes
Explanation: In this example, we create a structure Student to store a student's name, age, and grade. Each of the members (name, age, grade) is stored in its own separate memory location, and we access them individually. The size is also the size of all members combined plus structure padding.
Unions
A union in C is similar to a structure, but with a key difference: all members of a union share the same memory location. This means only one member of the union can store a value at any given time. The size of a union is determined by the size of its largest member.
Syntax:
union name {
member1 definition;
member2 definition;
...
memberN definition;
};
Example:
C
//Driver Code Starts
#include <stdio.h>
//Driver Code Ends
union Data {
int i;
double d;
char c;
};
int main() {
// Create a union variable
union Data data;
// Store an integer in the union
data.i = 100;
printf("%d
", data.i);
// Store a double in the union (this will
// overwrite the integer value)
data.d = 99.99;
printf("%.2f
", data.d);
// Store a character in the union (this will
// overwrite the double value)
data.c = 'A';
printf("%c
", data.c);
printf("Size: %d", sizeof(data));
//Driver Code Starts
return 0;
}
//Driver Code Ends
Output100
99.99
A
Size: 8
Similarities Between Structure and Union
Structures and unions are also similar in some aspects listed below:
- Both are user-defined data types used to store data of different types as a single unit.
- Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
- Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
- A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
- ‘.’ operator or selection operator, which has one of the highest precedences, is used for accessing member variables inside both the user-defined datatypes.
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts