The short answer

In C, a structure (struct) gives each member its own separate memory, so all members hold values at the same time and the size is the sum of the members plus padding. A union makes every member share the same memory, so its size equals the largest member, and only one member is valid at a time. In short, a struct stores many values together, while a union stores one value at a time to save space.

Structure and union are two user-defined types in C that group variables, called members, under one name. Both appear in every C course and the GATE syllabus. Students often confuse them, because the syntax looks almost identical.

The real difference is memory. A structure spreads its members across separate memory, while a union packs them into one shared location. As a result, a struct can hold all its fields together, whereas a union holds only one at a time. This guide defines each type, compares them in detail, and shows when to use which.

If you are still building your C basics, it also helps to know how functions like ftell and fseek work first.

Diagram showing a struct with members a, b, c in three separate memory cells (size equals the sum) beside a union with a, b, c sharing one overlapping cell (size equals the largest member)
A struct gives each member its own memory; a union makes all members share one block.

What is a Structure?

A structure is a user-defined type that groups related variables under one name. You define it with the struct keyword, and each variable inside is a member. The members can hold different types, such as an int, a float, and a char.

Crucially, each member gets its own separate memory. So every member keeps its value independently, and you can read or write all of them at the same time. Because the members sit side by side, the total size is roughly the sum of the member sizes, plus a little padding for alignment. As a result, a struct is the natural way to model a record, such as a student with a name, an age, and marks.

Advantages of a structure:

  • Groups related variables of different types into one clean unit.
  • Keeps every member’s value independently, so all fields coexist.
  • Lets you access all members at the same time without conflict.
  • Suits records, objects, and other complex data models.

Disadvantages of a structure:

  • Uses more memory, since each member takes its own space.
  • Adds padding bytes for alignment, which can waste a few bytes.
  • Wasteful when only one member is ever used at a time.

What is a Union?

A union is also a user-defined type that groups members, yet it stores them very differently. You define it with the union keyword, and the syntax mirrors a struct. However, all members share the same memory location.

Because the members overlap, the union’s size equals the size of its largest member, not the sum. So only one member holds a valid value at a time, and writing to one member overwrites the others. That overlap makes a union ideal for saving memory or for modelling a value that is one of several types, often called a variant. Embedded systems and other memory-constrained code use unions for exactly that reason.

Advantages of a union:

    • Saves memory, because all members share one block.
  • Needs space only for the largest member, not the sum.
  • Models a value that is one of several types at a time.
  • Fits embedded systems and other memory-constrained environments.

Disadvantages of a union:

  • Holds only one valid member at a time, so the others are lost.
  • Writing one member silently overwrites the rest.
  • Needs a separate flag to track which member is currently valid.

Structure vs Union: Comparison Table

Two-column comparison showing struct members in separate memory boxes versus union members sharing one box
Structure vs union at a glance: separate memory versus shared memory.
AspectStructureUnion
Keywordstruct defines a structureunion defines a union
Memory per memberEach member gets separate space in memoryAll members share the same memory space
Memory useEach member uses its own memoryAll members reuse one shared block
Total sizeSum of all members, plus paddingEqual to the largest member
Members held at onceAll members hold values togetherOnly one member is valid at a time
AccessAny individual member can be accessed any timeOnly one member can be accessed at a time
Writing a memberDoes not affect the other membersOverwrites whatever shared the memory
InitializationAll members can be initialized when declaredOnly one member can be initialized when declared
Memory efficiencyUses more memorySaves memory
PaddingAdds padding between members for alignmentPads only up to the largest member
Data integrityEvery member keeps its own value safelyOld values are lost when another is written
Syntaxstruct name { members };union name { members };
Typical useRecords and grouped fields that coexistVariants and memory-constrained code
ExampleA student with name, age, and marksOne value stored as int, float, or char
Common inGeneral application data modelsEmbedded systems and type-punning

How Memory Works in a Structure and a Union

The whole difference comes down to memory layout. So picture three members: an int, a float, and a char.

In a structure, those three members sit in three separate cells, one after another. Each cell holds its own value, and the compiler may add padding so each member starts on an aligned address. Therefore the total size is the sum of the members plus that padding. Because nothing overlaps, all three values stay safe together.

In a union, those same three members overlap one shared cell. The cell is only as big as the largest member, which is the float here. So writing the int and then the float reuses the same bytes, and the earlier int value is gone. That is why a union holds just one valid value at a time.

Diagram of a union where writing member b into the shared memory block overwrites the value previously written to member a
Writing one union member overwrites the others, because they share the same memory.

Code Walkthrough: sizeof and Overwrite

A short C example makes the contrast clear. First, here is a structure with three members.

#include <stdio.h>

struct Demo {
    int   integer;   // 4 bytes
    float x;         // 4 bytes
    char  c;         // 1 byte
};

int main(void) {
    struct Demo s;
    s.integer = 10;
    s.x = 3.14f;
    s.c = 'A';
    // All three coexist:
    printf("%d %.2f %c\n", s.integer, s.x, s.c);   // 10 3.14 A
    printf("size = %zu\n", sizeof(struct Demo));    // 12 (sum + padding)
    return 0;
}

Each member keeps its value, so the program prints all three. The sizeof is the sum of the members plus padding, which lands at 12 bytes on a typical compiler. Now compare the union version.

#include <stdio.h>

union Demo {
    int   integer;   // 4 bytes
    float x;         // 4 bytes
    char  c;         // 1 byte
};

int main(void) {
    union Demo u;
    u.integer = 10;
    u.x = 3.14f;     // overwrites the same memory
    // u.integer is now garbage, because x reused its bytes:
    printf("%.2f\n", u.x);                         // 3.14
    printf("size = %zu\n", sizeof(union Demo));     // 4 (largest member)
    return 0;
}

Here the size is 4 bytes, the size of the largest member. Moreover, writing u.x after u.integer overwrites the shared bytes, so the integer value is no longer meaningful. That single behaviour captures the entire structure vs union trade-off.

When to Use a Structure or a Union

The choice depends on whether your members must coexist. So match the type to the data, not to habit.

Use a structure when several related values belong together and must all stay valid. A student record with a name, an age, and marks is the classic case, since you need every field at once. Most everyday data models fit here.

Use a union when only one of several values is active at any moment, or when memory is tight. A value that might be an int, a float, or a char, such as a token in a parser, suits a union well. Embedded systems also lean on unions to squeeze data into limited memory.

In practice, programmers often combine both. A common pattern wraps a union inside a struct, with one extra field that flags which union member is currently valid. That tagged union gives you the memory savings of a union and the safety of a struct.

Interview Questions

Because all members of a union share the same memory location, the compiler only needs enough space to hold the biggest one. So the size equals the largest member, not the sum. A struct, by contrast, places members in separate memory, so its size is the sum plus padding.

You usually get garbage, because the members share the same bytes. Writing one member overwrites whatever the others held. So reading a different member reinterprets those raw bytes, which is meaningful only in deliberate type-punning. Therefore you should track which member is currently valid.

The compiler aligns each member to a natural boundary so the CPU can access it efficiently. As a result, it inserts padding bytes between members or at the end. That is why a struct’s size can exceed the plain sum of its members. A union pads only up to its largest member.

A tagged union is a struct that holds a union plus a small tag field. The tag records which union member is currently valid, so you read the right one safely. Therefore it combines a union’s memory savings with the safety of a struct. Many parsers and interpreters use this pattern.

Frequently Asked Questions

A structure in C lets you group variables of different types into a single unit. Each member stores its value in its own separate memory space, so you can access them all independently. As a result, a struct is ideal for records such as a student with a name, an age, and marks.

A union in C lets multiple members share the same memory location. So it stores only one member value at a time, because all members overlap in memory. Therefore its size equals the largest member, which makes a union handy when memory is tight.

There are four main differences. In terms of memory, a struct gives each member its own location, while a union shares one. As for size, a struct equals the sum of its members, whereas a union equals the largest. For usage, a struct stores many values at once, but a union stores one at a time. With access, a struct exposes all members, while writing one union member affects the others.

A structure groups related variables of different types into one unit. Moreover, each member preserves its own value independently in its own memory. So structures suit applications that need complex data models, such as records or objects.

A union saves memory by letting multiple members share the same memory. So it works best when only one value needs storage at a time. Because of that efficiency, you often use unions in embedded systems and other memory-constrained environments.

You use the struct keyword, a tag name, and the members in braces, like so:

struct Example {
    int   a;
    float b;
    char  c;
};

After that, you create a variable with struct Example e; and set each member with e.a, e.b, and e.c.

The syntax mirrors a struct, but with the union keyword:

union Example {
    int   a;
    float b;
    char  c;
};

However, remember that a, b, and c share the same memory, so only one is valid at any moment.

Neither is simply better, because it depends on the use case. Use a structure to store and access multiple values at the same time. Use a union to optimise memory when only one value is required at a time. So the data decides, not the keyword.

Wrapping Up

Structure and union look alike, yet they manage memory in opposite ways. A struct gives each member its own space, while a union makes every member share one block.

Remember the simple rule: a struct is the sum of its members and holds them all, whereas a union is the size of its largest member and holds just one. So reach for a struct when fields must coexist, and reach for a union when you need to save memory. That single contrast answers most exam and interview questions on the two.

Related reading on DiffStudy:


Whatsapp-color Created with Sketch.

By Arun Kumar

Full Stack Developer with a BE in Computer Science, working with React, Next.js, Node.js, MongoDB, and AI/ML tools. Founder of DiffStudy — built to help CS students ace GATE and university exams, and keep developers up to date across AI, cloud, system design, web development, and every field of computer science. Every article is written from real hands-on experience, not just theory.

2 thought on “Structure vs Union in C: Key Differences”

Leave a Reply

Your email address will not be published. Required fields are marked *


You cannot copy content of this page