Kit Library / Computer Science / C Programming / Memory Management

Quick Kit

Structures and Unions in C

En 12 leveled MCQs 22 flashcards 4 games Free

Shared by a Veda learner · Generated with Veda AI

⚡ Veda Bites

The whole idea, one bite at a time

Veda Bites are swipeable micro-lessons — each one teaches exactly one idea. Here's a taste from this kit; the app has the full deck.

💡 Key Idea

Core Difference in One Line

Structs side-by-side, unions overlap.

A struct stores all members side-by-side, each with its own memory. A union stores all members in the same memory space, so only one member is valid at a time.

↳ Structs keep all data accessible; unions save memory by sharing space.

📖 Definition

What is a Structure?

Bundle different types into one.

A structure (`struct`) is a user-defined data type that groups different types of variables under one name. All members exist at the same time, each with its own memory location.

↳ A struct is a collection of related but distinct data fields.

📖 Definition

What is a Union?

Different types, same memory spot.

A union (`union`) allows storing different types in the same memory location. Only one member is active at a time; all members share the same starting address.

↳ A union is a single memory space that can hold one of several types.

📖 Smart notes

What you'll study, topic by topic

1

Structures vs Unions in C: Memory, Usage, and Key Differences

Structures and unions in C are both ways to group data, but they differ fundamentally in memory layout and usage. Structures allocate separate memory for each member, allowing simultaneous access, while unions share memo...

  • A struct stores all members side-by-side, each with its own memory; a union stores all members in the same memory space.
  • Struct size is the sum of all members plus padding; union size is the size of the largest member.
  • In a struct, all members are valid simultaneously; in a union, only one member is valid at a time.

~8 min · full explanation, examples & memory tricks in the app

❓ Leveled MCQ practice

Try the smart MCQs from this kit

12 questions laddered from warm-up to topper-level, each with an explanation. A taste:

What is the primary difference between a `struct` and a `union` in C?

Beginner
A A union is used for grouping related data; a struct is used for saving memory. B A struct stores all members in the same memory location; a union stores them separately. C A struct stores all members side-by-side with separate memory; a union stores all members in the same memory space. D A struct can only store one type of data; a union can store multiple types.
Show answer & explanation

A struct stores all members side-by-side with separate memory; a union stores all members in the same memory space.

The core difference is that a struct allocates separate memory for each member, while a union shares a single memory space among all members.

Given `struct S { int a; char b; };` and assuming `int` is 4 bytes and `char` is 1 byte, what is the minimum possible size of `struct S` (ignoring padding)?

Beginner
A 5 bytes B 8 bytes C 4 bytes D 1 byte
Show answer & explanation

5 bytes

The size of a struct is at least the sum of its members' sizes: 4 + 1 = 5 bytes (padding may increase it).

Given `union U { int a; char b; };` and assuming `int` is 4 bytes and `char` is 1 byte, what is the size of `union U`?

Beginner
A 4 bytes B 5 bytes C 8 bytes D 1 byte
Show answer & explanation

4 bytes

A union's size equals the size of its largest member, which is the `int` at 4 bytes.

In a `union`, what happens when you write to one member after another member has been assigned?

Intermediate
A The previous member's value is overwritten. B The union automatically resizes. C The previous member's value is preserved. D The program crashes.
Show answer & explanation

The previous member's value is overwritten.

All members of a union share the same memory location, so writing to one overwrites the data of any previously active member.

🃏 Flashcards

Tap a card to flip it

22 flashcards in this kit — the app reviews them with spaced repetition so the right card returns on the right day.

🎮 Learning games

Play your way through this kit

Every game is built from this kit's own content — scores feed your mastery, so playing counts as studying.

Word Match Word Scramble Sequence Builder Categorization Playable in the app

Study it properly — free, in the app

The full Veda Bites deck, complete notes, spaced-repetition flashcards, leveled MCQs, tests and games for this kit — plus Daily Facts and the Arena, every day.