Kit Library / Computer Science / C Programming / Memory Management

⚡ Topic Learning Kit

Structures and Unions in C

English 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. The bites below come from this kit — swipe through them in the app.

💡 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.

⚖️ Comparison

Memory Size: Struct vs Union

Sum vs largest member.

↳ Structs use more memory but keep all data; unions use less but only one value at a time.

✏️ Example

Struct Example in Action

Student record with all fields.

↳ In a struct, you can read and write all members independently.

✏️ Example

Union Example in Action

One value at a time.

↳ Writing to one union member overwrites the others.

🔗 Analogy

Toolbox vs Single Drawer

Separate slots vs one drawer.

A struct is like a toolbox with separate compartments — every tool has its own slot. A union is like a single drawer — put in a hammer, the wrench is gone.

↳ Structs keep all tools; unions hold only one tool at a time.

🛠️ Application

When to Use Struct

Keep all fields together.

Use `struct` when you need to keep all fields together and access them at the same time. Examples include a date (day, month, year), a student record, or a point (x, y).

↳ Structs are for grouping related data you need simultaneously.

🛠️ Application

When to Use Union

One of several types.

Use `union` when you have a variable that can hold one of several types but never more than one at once. Examples include a token in a parser (number, string, or operator) or a network packet payload.

↳ Unions are for memory-efficient variants where only one type is active.

⚠️ Common Mistake

Reading the Wrong Union Member

Don't read inactive members.

↳ Always track which union member is currently active.

✅ Best Practice

Tagged Unions

Add a flag to track type.

To safely use a union, pair it with a tag (an enum or int) that indicates which member is active. This is called a *tagged union* or *discriminated union*.

↳ Always use a tag to know which union member is valid.

➗ Formula

Size Calculation Formula

Sum vs max.

↳ Struct size is sum plus padding; union size is the largest member.

✏️ Example

Quick Size Comparison

See the size difference.

↳ Unions can save significant memory when only one field is used at a time.

🌍 Real World

Real-World: Network Packets

Unions in packet parsing.

Network protocols often use unions to represent packet headers that can be of different types (e.g., TCP, UDP, ICMP). The union holds the specific header type, saving memory and allowing flexible parsing.

↳ Unions are used in systems programming for memory-efficient variant handling.

🌍 Real World

Real-World: Compiler Tokens

Unions in lexical analysis.

In compilers, a token can be a number, string, or operator. A union stores the token's value, while a separate tag indicates the token type. This is a classic *tagged union* pattern.

↳ Tagged unions are essential for implementing variants in C.

🎯 Exam Tip

Exam Tip: Size Questions

Remember sum vs max.

In exams, when asked for the size of a struct, add up all members and consider padding. For a union, just take the largest member's size. Don't forget padding for structs!

↳ For size questions: struct = sum + padding, union = max.

🧠 Memory Trick

Memory Trick: S for Separate, U for United

S = separate, U = united.

Struct = Separate memory for each member. Union = United (shared) memory. This helps recall the fundamental difference.

↳ S for separate, U for united — easy to remember.

❓ Did You Know

Did You Know?

Unions can save memory.

Unions are often used in embedded systems where memory is limited. By sharing memory, they allow a single variable to hold different data types without wasting space.

↳ Unions are a memory-saving tool in resource-constrained environments.

🔄 Workflow

How to Choose: Struct or Union?

Ask: need all at once?

↳ Choose struct for simultaneous access, union for memory-efficient variants.

🚫 Warning

Warning: Overwriting in Unions

Writing overwrites other members.

↳ Unions are not for storing multiple values simultaneously.

Smart notes

What you'll study, topic by topic

1

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

8 min

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.

Full explanation, worked examples & memory tricks in the app

Leveled MCQ practice

Try the smart MCQs from this kit

All 12 questions, laddered from warm-up to topper-level, each with its answer and the reasoning behind it.

Warm-up · 7 questions

Q1. 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.

Answers and explanations are in the Veda app.

Q2. 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

Answers and explanations are in the Veda app.

Q3. 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

Answers and explanations are in the Veda app.

Q4. Which of the following statements about `struct` members is true?

Beginner
A Only one member can be accessed at a time.
B Members must all be of the same type.
C All members are valid simultaneously.
D Members share the same memory address.

Answers and explanations are in the Veda app.

Q5. Which analogy best describes a `union`?

Beginner
A A toolbox with separate compartments.
B A bookshelf with multiple shelves.
C A single drawer where you put one tool at a time.
D A filing cabinet with many folders.

Answers and explanations are in the Veda app.

Q6. In a `union`, all members are valid at the same time.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Q7. A `struct` can contain members of different data types.

Beginner
A True
B False

Answers and explanations are in the Veda app.

Building up · 5 questions

Q8. 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.

Answers and explanations are in the Veda app.

Q9. Which of the following is a typical use case for a `union`?

Intermediate
A Storing multiple arrays of different sizes simultaneously.
B Storing a variable that can hold one of several types at a time, like a token in a parser.
C Storing a date with day, month, and year.
D Storing a student record with ID, name, and GPA.

Answers and explanations are in the Veda app.

Q10. What is the memory size of a `union` compared to a `struct` with the same members?

Intermediate
A Always larger than the struct.
B Equal to the size of the largest member, which is often smaller than the struct.
C Always the same as the struct.
D Equal to the sum of all members, same as the struct.

Answers and explanations are in the Veda app.

Q11. When would you prefer a `struct` over a `union`?

Intermediate
A When you have a variable that can hold different types at different times.
B When you need to save memory by storing only one of several types.
C When you want to overwrite data frequently.
D When you need to keep all fields together and access them simultaneously.

Answers and explanations are in the Veda app.

Q12. In the example `union Value { int i; float f; char str[20]; };`, what is the size of the union?

Intermediate
A 20 bytes
B 8 bytes
C 28 bytes
D 4 bytes

Answers and explanations are in the Veda app.

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

Built from this kit's own content

Word Scramble

Built from this kit's own content

🎮

Sequence Builder

Built from this kit's own content

🎮

Categorization

Built from this kit's own content

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.