Back to Course
Data Structures

Difference Between Structure and Union in C Language

What is a Structure?

A struct groups variables of different data types under one name, with each member having its own memory space.

struct Student {
    int id;
    char name[20];
};

What is a Union?

A union also groups variables of different data types under one name, but all members share the same memory location.

union Data {
    int i;
    float f;
};

Key Differences

  • Memory: struct allocates separate memory for each member; union allocates memory equal to its largest member, shared by all.
  • Access: in a struct, all members can hold valid values simultaneously; in a union, only one member holds a valid value at a time.
  • Size: sizeof(struct) is the sum of all members' sizes (plus padding); sizeof(union) equals its largest member's size.

Ready to master real-world C Programming development?

Learn C Programming hands-on with mentor-led, live sessions.

Explore Course