Difference between Structure and Union in C

Both Structure and Union are used to group different data types to organize data structurally. In C Programming, Structure and Union are similar in syntax with keyword differences. It means creating these variables to access their respective members is the same as keyword differences.

In the previous post, we already explained these concepts with multiple examples, so please refer to them. This article shows you the difference between Structure and Union in C Programming with an example.

What is the main difference between structure and union in C?

Before going into a practical example, let us see their differences.

StructuresUnion
Struct keyword is used to declare the structureUnion keyword is used to declare it
The structure variable will allocate memory for all the structure members separately.The 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
The union will occupy less memory space compared to structures.Memory_Size = Size of the largest Union member. From the above example, the Largest member is the 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 the Difference between Structure and Union

In this program, we are going to declare the structure and union with the same data type members. Then we are going to calculate the size using the sizeof function. Before these differences, I suggest you refer to Struct and Union articles in this Programming language.

#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;
}
Difference between Structure and Union in C programming Example

If you observe the above, both the Employee and Person hold the same data type and the same size. Within the main() function, we created the Employee structure variable emp1 using the below statement.

struct Employee emp1;

Next, We created the Person Union variable Person1 using the below statement

union Person Person1;

Next, within the printf statement, we used the C sizeof operator to calculate the size of both the 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 => 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

The largest member from the above is Name[50]. As we all know

Memory Size = Largest union number size = 50 Bytes.

It’s again 2 extra bytes for padding.

Size of structure vs size of union with padding

In C programming, the size of a structure and the size of a union differ because of their memory allocations.

Size of a structure

When we declare a structure, each member gets its own memory, and there will be padding for the alignment after a member or at the end.

In the following example, we declared a structure with int and double members. Here, the integer member is of 4 bytes and the double is of 8 bytes. The total is 12 bytes, and there will be padding between them or at the end, so the size of the structure is 16.

#include <stdio.h>
struct Sample
{
int i;
double d;
};

int main()
{
printf("%zu\n", sizeof(struct Sample));
return 0;
}
16

Size of a union

When we declare a union, all its members get a shared memory. It takes the size of the largest union member, and that memory will be shared by all its members. Here, there will be no padding.

Here, we use the same example and replace the structure with a union. If you observe the output, it returns the size of the union as 8 bytes.

Here, the largest data type is double, and its size is 8 bytes, so the union allocates shared memory of 8 bytes. Next, the int variable uses the first 4 bytes, and the double uses all 8 bytes. It means there will be a memory overlap.

#include <stdio.h>
union Sample
{
int i;
double d;
};

int main()
{
printf("%zu\n", sizeof(union Sample));
return 0;
}
8

Comments are closed.