In the previous post, we already explained about the Structure and Union. Please refer to Structure and Union posts before going into the differences.
- Both are used to group different data types to organize data structurally.
- Structure and Union are similar in syntax with keyword differences.
- Creating Structure variable and Union variable to access their respective members is the same with keyword difference.
In this article, we show you the difference between Structures and Union in C Programming with example. Before going into a practical example, let us see the differences between structure and union.
Structures | Union |
---|---|
Struct keyword is used to declare the structure | Union keyword is used to declare the Union |
Structure variable will allocate memory for all the structure members separately. | Union variable will allocate common memory for all the union members. |
Example: struct Employee{ int age; char name[50]; float salary; }; | Example: union Employee{ int age; char name[50]; float salary; }; |
Structures will occupy more memory space.Memory_Size = addition of all the structure members sizes. Memory_Size = int + char array [50] + float Memory_Size = 2 + 50 + 4 Bytes Memory_Size = 56 Byte | Union will occupy less memory space compared to structures.Memory_Size = Size of the largest Union member. From the above example, the Largest Union member is char array. So, Memory_Size = 50 Bytes |
It allows us to access any or all the members at any time. | It allows us to access only one union member at a time. |
C Program to find Difference between Structure and Union
In this C program, we are going to declare the structure and union with the same data type members. Then we are going to calculate the size of union and structure using the sizeof function. I suggest you refer to Structure and Union articles in C Programming.
/* C Program to find difference between Structure and Union */ #include <stdio.h> struct Employee { int age; char Name[50]; char Department[20]; float Salary; }; union Person { int ag; char Nam[50]; char Departent[20]; float Salar; }; int main() { struct Employee emp1; union Person Person1; printf(" The Size of Employee Structure = %d\n", sizeof (emp1) ); printf(" The Size of Person Union = %d\n", sizeof (Person1)); return 0; }
OUTPUT
ANALYSIS
Declared the Employee structure with structure members
int Age; char Name[50]; char Department[20]; float Salary;
Next, We declared the Person Union with Union Members
int Age; char Name[50]; char Department[20]; float Salary;
If you observe the above, both the Employee structure and Person Union hold the same data type and same size.
Within the main() function, we created Employee structure variable emp1 using below statement
struct Employee emp1;
Next, We created Person Union variable Person1 using below statement
union Person Person1;
Next, within the printf statement, we used the sizeof operator to calculate the size of both Employee structure and Person Union.
sizeof (emp1) = 80
int Age; - It will take 4 bytes of space in the memory char Name[50]; - It will take 50 bytes of space in the memory char Department[20]; - It will take 20 bytes of space in the memory float Salary; - It will take 4 bytes of space in the memory
Memory Size of a Structure = 4 + 50 + 20 + 4 Bytes
Memory Size of a Structure = 78 Bytes
If you observe the output, it has given 80 Bytes. Because of the structure padding, it has taken an extra 2 Bytes. We will discuss the structure padding in the coming posts.
sizeof (Person1) = 52 Bytes
Largest union number from the above is Name[50], As we all know
Memory Size of a Union = Largest union number size
Memory Size of a Union = 50 Bytes
It’s again 2 extra bytes for padding