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.
Structures
A structure is a user-defined data type that groups variables of different data types under a single name.
- Each structure member has its own separate memory location.
- The size of a structure is approximately the sum of the sizes of all its members (including any padding added by the compiler).
#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;
}
Output
Geek 20 85.50 Size: 60 bytes
Syntax
struct name {
member1 definition;
member2 definition;
...
memberN definition;
};
Unions
A union is a user-defined data type in which all members share the same memory location, allowing only one member to hold a valid value at a time.
- The size of a union is equal to the size of its largest data member.
- Unions help save memory by storing different data types in the same memory location.
//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
Output
100 99.99 A Size: 8
Syntax
union name {
member1 definition;
member2 definition;
...
memberN definition;
};
Structure Vs Union
| Structure | Union |
|---|---|
| A user-defined data type that groups different data types into a single entity. | A user-defined data type that stores different data types in the same memory location. |
Defined using the struct keyword. | Defined using the union keyword. |
| Size is the sum of all member sizes (including padding if required). | Size is equal to the size of the largest member (including padding if required). |
| Each member has its own separate memory location. | All members share the same memory location. |
| Members do not overlap in memory. | Members completely overlap in memory. |
| All members can store and retain their values simultaneously. | Only one member holds a valid value at a time. |
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.