A union is a special data type in C that allows storing different data types in the same memory location. Only one member can hold a value at a time.
Click to reveal answer
beginner
How does memory allocation work in a union?
The size of a union is equal to the size of its largest member because all members share the same memory space.
Click to reveal answer
intermediate
What happens if you assign a value to one member of a union and then read from another member?
Reading from a different member than the one last assigned can lead to unexpected or garbage values because all members share the same memory.
Click to reveal answer
beginner
Write a simple union declaration with an int and a float member.
Example:
union Number {
int i;
float f;
};
Click to reveal answer
intermediate
Why might you use a union instead of a struct?
You use a union to save memory when you know only one of several types will be used at a time, unlike a struct which allocates memory for all members.
Click to reveal answer
What is the size of a union with members: char c;, int i;, and double d;?
ASum of sizes of all members
BSize of <code>int</code>
CSize of <code>double</code>
DSize of <code>char</code>
✗ Incorrect
A union's size is the size of its largest member, here double.
If you store a value in one union member and then read from another, what is the result?
AUndefined or garbage value
BZero
CCompiler error
DThe value of the last stored member
✗ Incorrect
Reading a different member than the one last assigned leads to undefined or garbage values.
Which keyword is used to declare a union in C?
Aunion
Bstruct
Cenum
Dtypedef
✗ Incorrect
The keyword union declares a union type.
Why use a union instead of a struct?
ATo define functions
BTo store multiple values at once
CTo create arrays
DTo save memory when only one value is needed at a time
✗ Incorrect
Unions save memory by sharing space for members used one at a time.
How do you access members of a union variable named u?
A<code>u->member</code>
B<code>u.member</code>
C<code>*u.member</code>
D<code>u:member</code>
✗ Incorrect
Use dot notation u.member to access union members.
Explain what a union is in C and how it differs from a struct.
Think about memory usage and how values are stored.
You got /3 concepts.
Describe a situation where using a union would be more efficient than a struct.
Consider saving memory in embedded systems or low-memory devices.
You got /3 concepts.
Practice
(1/5)
1. What is the main characteristic of a union in C?
easy
A. Unions can only store integers
B. Each member has its own separate memory
C. All members share the same memory location
D. Unions automatically initialize all members
Solution
Step 1: Understand union memory sharing
A union allocates one shared memory block for all its members, so they overlap.
Step 2: Compare with struct memory allocation
Unlike structs, unions do not allocate separate memory for each member.
Final Answer:
All members share the same memory location -> Option C
Quick Check:
Union memory sharing = All members share the same memory location [OK]
Hint: Remember: union members overlap in memory [OK]
Common Mistakes:
Thinking each member has separate memory
Assuming unions initialize all members
Believing unions only store one data type
2. Which of the following is the correct way to define a union named Data with an int and a float member?
easy
A. union Data { int i; float f; };
B. struct Data { int i; float f; };
C. union Data ( int i; float f; );
D. union Data = { int i; float f; };
Solution
Step 1: Recall union syntax
The correct syntax uses the keyword 'union', the name, and braces with members inside.
Step 2: Check each option
union Data { int i; float f; }; uses correct syntax with braces and semicolons. Options A, B, and C have syntax errors or wrong keywords.
Final Answer:
union Data { int i; float f; }; -> Option A
Quick Check:
Correct union syntax = union Data { int i; float f; }; [OK]
Hint: Use 'union Name { members; };' syntax [OK]
Common Mistakes:
Using struct instead of union
Using parentheses instead of braces
Assigning values in the definition
3. What will be the output of the following C code?
union Number {
int i;
float f;
};
int main() {
union Number num;
num.i = 65;
printf("%c", num.f);
return 0;
}
medium
A. A
B. 65
C. Compilation error
D. Undefined behavior or garbage character
Solution
Step 1: Understand union member sharing
Setting num.i = 65 stores integer bits in union memory.
Step 2: Printing num.f as char
Printing num.f with %c treats float bits as a char, causing unpredictable output.
Final Answer:
Undefined behavior or garbage character -> Option D
Quick Check:
Accessing different union member types = Undefined behavior or garbage character [OK]
Hint: Accessing different union members can cause garbage output [OK]
Common Mistakes:
Expecting integer 65 to print as 'A'
Assuming union members convert automatically
Ignoring type mismatch in printf
4. Identify the error in this union usage:
union Data {
int i;
float f;
};
int main() {
union Data d;
d.i = 10;
d.f = 3.14;
printf("%d", d.i);
return 0;
}
medium
A. Using %d to print a union member is invalid
B. Assigning to d.f overwrites d.i value
C. Cannot assign to multiple union members sequentially
D. Union must be initialized before use
Solution
Step 1: Analyze assignments to union members
Assigning d.i then d.f means d.f overwrites the same memory used by d.i.
Step 2: Understand printf output
Printing d.i after assigning d.f prints the overwritten value, not 10.
Final Answer:
Assigning to d.f overwrites d.i value -> Option B
Quick Check:
Union members share memory, last assignment wins = Assigning to d.f overwrites d.i value [OK]
Hint: Last assigned union member overwrites previous values [OK]
Common Mistakes:
Thinking union members hold values independently
Believing %d cannot print union members
Assuming union needs initialization
5. You want to store either an int, a float, or a char in the same memory space and print the stored value correctly. Which approach using a union and an additional variable is best?
hard
A. Use a union for data and an enum to track the active member type
B. Use a union and print all members every time
C. Use separate variables for each type without union
D. Use a union and always print the int member
Solution
Step 1: Understand union limitation
Union stores one member at a time, so we must know which member is active.
Step 2: Use enum to track active member
An enum variable can track which type is stored, so printing uses correct format.
Final Answer:
Use a union for data and an enum to track the active member type -> Option A
Quick Check:
Union + enum for type tracking = Use a union for data and an enum to track the active member type [OK]
Hint: Track active union member with enum for correct printing [OK]
Common Mistakes:
Printing all union members blindly
Not tracking which member is active
Using separate variables wastes memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.