Difference Between Structure and Union in C
Contents
Structure and Union Comparison
The Key Difference Between Structure and Union is that Both the structure and union are user-defined data types in C Language. Structure is a collection of logically related data items of different data types grouped together under a single name. Union is a user-defined data type just like structure.

Comparison Chart
Structure | Union |
---|---|
Keyword struct defines a structure | Keyword union defines a union. |
Each member is assigned its own unique storage area. | All members share the same storage area. |
Total memory required by all members is allocated. | Maximum memory required by the member is allocated. |
All members are active at a time. | Only one member is active time. |
All members can be initialized. | Only the first member can be initialized. |
Requires more memory. | Requires less memory. |
No Anonymous feature. | Anonymous union can be declared. |
Example:
struct SS
{
int a;
float b;
char c;
};
1 byte for c |
Example:
union UU
{
int a;
float b;
char c;
};
4 bytes for c,b,a |
Structure
- Structure is a collection of logically related data items of different data types grouped together under a single name.
- Structure is a user-defined data type.
- Structure helps to organize complex data in a more meaningful way.
Syntax of Structure
struct structure_name
{
data_type member1;
data_type member2;
………….
};
- struct is a keyword.
- structure_name is the tag name of a structure.
- member1, member2 are members of a structure.
Example
#include<stdio.h>
#include<Conio.h>
struct book
{
char title[100];
char author[50];
int pages;
float price;
};
void main()
{
struct book book1;
printf("enter title, author name, pages and price of book");
scanf(“%s”,book1.title);
scanf(“%s”, book1.author);
scanf("%d",&book1.pages);
scanf("%f",&book1.price);
printf("\n detail of the book");
printf(“%s”,book1.title);
printf(“%s”,book1.author);
printf("%d",book1.pages);
printf("%f",book1.price);
getch();
}
- book is a structure whose members are title, author, pages, and price.
- book1 is a structure variable.
Union
- Union is a user-defined data type just like structure.
- Each member in the structure is assigned its own unique storage area whereas, in Union, all the members share a common storage area.
- All members share the common area so only one member can be active at a time.
- Unions are used when all the members are not assigned value at the same time.
Example
union book
{
char title[100];
char author[50];
int pages;
float price;
};