Theory
One keyword, two philosophies
Last lesson ended with a puzzle: struct and union share IDENTICAL syntax, braces, members, dots, yet behave like different species.
Exams adore this pair: "Differentiate between structure and union" appears so reliably that seniors call it free marks.
Today: the comparison table, the sizeof proof, and the one sentence that untangles the two forever. The sentence involves a hostel.
Theory
Separate rooms vs one shared room
A struct is a hostel wing: every member gets their OWN room, all can live there at the SAME time, and the wing's size is the SUM of the rooms. A union is a single room with a rotating nameplate: whoever moves in throws the previous occupant's luggage out. Its size? Just big enough for the LARGEST possible occupant. Same building form; opposite housing policy.
At a glance
The comparison exams demand
| Aspect | Structure | Union |
|---|---|---|
| Memory | Each member has own space | All members share ONE block |
| Size | Sum of members (+ padding) | Largest member only |
| Alive at once | All members simultaneously | Only the last-written member |
| Reading others | Always valid | Garbage (overwritten bytes) |
| Best for | Records: student, employee | Memory-scarce, one-of-many values |
Practical
The sizeof proof
#include <stdio.h>
struct sBox { int i; float f; char c; };
union uBox { int i; float f; char c; };
int main() {
printf("struct: %lu bytes\n", sizeof(struct sBox));
printf("union : %lu bytes\n", sizeof(union uBox));
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Predict the two sizes
Same three members: int (4), float (4), char (1). Reason out both sizeof results before revealing. (Hint: sum for one, largest for the other, and C may pad.)
Show the answer
struct: 4 + 4 + 1 = 9, padded by most compilers to 12 (members aligned to 4-byte boundaries). union: the largest member is 4 bytes, so 4.
Write "sum of members, possibly padded" vs "size of largest member" and the sizeof question is fully earned. The 12-vs-4 gap IS the difference, in numbers.
Quiz
When is a UNION the right choice over a struct?
- When a variable holds only ONE of several types at a time and memory is precious
- Whenever you want faster access to members
- When all members must hold values together
- Never: unions are obsolete
Show the answer
When a variable holds only ONE of several types at a time and memory is precious
The union's one talent is thrift: a sensor reading that is EITHER an int OR a float (never both) needs only the larger space, not the sum, gold in embedded systems and device programming. Needing members TOGETHER (option C) is precisely the struct's job. Choice questions here test exactly this either/or judgement.
Quiz
struct sBox s; s.i = 10; s.f = 2.5; printf("%d", s.i); versus the same sequence on union uBox u. What prints in each case?
- struct prints 10; union prints garbage
- Both print 10
- Both print garbage
- struct prints garbage; union prints 10
Show the answer
struct prints 10; union prints garbage
In the struct, i and f live in SEPARATE rooms: writing f cannot disturb i, so 10 survives. In the union they are one room: u.f = 2.5 evicted the 10, and reading u.i reinterprets float bytes, garbage. This paired trace is the cleanest possible answer to "demonstrate the difference with an example".
Watch out
Where marks leak
Writing exact struct sizes without mentioning padding: say "sum of members, plus possible alignment padding", the honest full-mark phrasing. Claiming unions are faster: their virtue is SIZE, not speed. Initializing a union with multiple values: only the FIRST member can be initialized ({10} works, {10, 2.5} does not). And in the table answer, always include the use-case row, that is where the fifth mark hides.
Theory
The subject, complete
Stand back and look: the marks program began printing one hard-coded line, and ends as an array of student records it can read, grade, sort and report, using every construct in the syllabus. That program IS your revision: rebuild it from memory before the exam, and each unit re-teaches itself. Next semester, Python (BCA204) will make you smile at how much you already understand.
Summary
Key takeaways
- Struct: separate memory per member; size = sum (+ padding); all alive together.
- Union: one shared block; size = largest member; only the last-written member is valid.
- sizeof proof: {int, float, char} → struct ~12 (padded), union 4.
- Union initialization touches only the first member.
- Choose struct for records (default); union for one-of-many values under memory pressure.
- Memory hook: separate rooms vs one room with a rotating nameplate.