0% found this document useful (0 votes)
2K views1 page

Diff. Between Structure and Union

Structures and unions in C both define a custom data type that can have multiple members of different types. Structures allocate separate memory for each member, while unions allocate shared memory so only one member can be accessed at a time. The size of a structure is the sum of its members, while a union size equals its largest member.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views1 page

Diff. Between Structure and Union

Structures and unions in C both define a custom data type that can have multiple members of different types. Structures allocate separate memory for each member, while unions allocate shared memory so only one member can be accessed at a time. The size of a structure is the sum of its members, while a union size equals its largest member.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Difference between Structure and Union

STRUCTURES UNION

Struct keyword is used to declare the Union keyword is used to declare


structure the Union

Structure variable will allocate memory Union variable will allocate common
for all the structure members separately. memory for all the union members.

Example:
Example:
union Employee{
struct Employee{
int age;
int age;
char name[50];
char name[50];
float salary;
float salary;
};
};
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.

Members do not share memory in a Members share the memory space in a


structure. union.

Size of the structure is equal to the sum of


size of the each member Size of the union is equal to the size of
the largest member.

You might also like