0
0
Cprogramming~10 mins

Union basics - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Union basics
Declare union variable
Assign value to one member
Access value via any member
Value reflects last assignment
Memory shared among members
A union lets you store different data types in the same memory space, but only one at a time. Assigning to one member changes the shared memory.
Execution Sample
C
union Data {
  int i;
  float f;
};

union Data d;
d.i = 10;
This code declares a union with int and float, creates a variable, and assigns 10 to the int member.
Execution Table
StepActionMember AccessedValue StoredMemory State (shared)
1Declare union variable dN/AN/AUninitialized
2Assign 10 to d.ii10Memory holds int 10
3Access d.ii10Memory holds int 10
4Access d.ffBit pattern of int 10 as floatSame memory, interpreted as float
💡 Execution stops after showing union memory shared and value interpretation
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4
d.iundefined101010
d.fundefinedbit pattern of 10 as floatbit pattern of 10 as floatbit pattern of 10 as float
Key Moments - 2 Insights
Why does accessing d.f after assigning d.i show a strange float value?
Because union members share the same memory, the bits of the int 10 are reinterpreted as a float, not converted. See execution_table step 4.
Can we store int and float values at the same time in a union?
No, only one member's value is stored at a time since all members share the same memory space.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what value is stored in d.i?
AUndefined
B0
C10
DBit pattern of float
💡 Hint
Check the 'Value Stored' column at step 2 in execution_table
At which step does the union memory hold the int value 10?
AStep 2
BStep 4
CStep 1
DNever
💡 Hint
Look at the 'Memory State' column in execution_table
If we assign a float value to d.f after step 2, what happens to d.i?
Ad.i keeps the old int value
Bd.i value is overwritten by float bits
Cd.i becomes the float value
Dd.i and d.f store values independently
💡 Hint
Recall union shares memory; assigning to one member overwrites others
Concept Snapshot
union TypeName {
  type1 member1;
  type2 member2;
};

- All members share the same memory space.
- Only one member holds a valid value at a time.
- Assigning to one member overwrites others.
- Useful to save memory or interpret data differently.
Full Transcript
This visual trace shows how a union in C works. First, we declare a union with int and float members. Then we create a variable of that union type. When we assign 10 to the int member, the union's shared memory holds that int value. Accessing the int member returns 10 as expected. However, accessing the float member reads the same memory bits but interprets them as a float, resulting in a strange float value. This happens because all union members share the same memory space, so only one value is stored at a time. Assigning to one member overwrites the memory for all members. This helps save memory and allows different views of the same data.