Explore how unions in C enable efficient memory management by storing different data types in the same memory location. This quiz assesses your understanding of union syntax, behavior, practical applications, and differences from structures, helping you master union-related programming concepts.
When a union in C contains an int, a float, and a char array of size 8, what is the size of the union likely to be on a system where int and float are 4 bytes each and char is 1 byte?
Explanation: The size of a union in C is determined by its largest member, which in this case is the char array of size 8 bytes. Even though the int and float are each 4 bytes, the compiler allocates enough space to fit the largest member, not the sum. 13 bytes is incorrect because sizes are not additive in unions. 4 bytes is only enough for the int or float, but not for the array. 16 bytes does not match any member's size, and would waste memory.
If you assign a value to the float member of a union and then immediately read the int member, what is likely to happen?
Explanation: Writing to one member of a union affects the values of all other members, since they share the same memory. Reading a different member right after writing will show unpredictable or 'garbage' data. The int does not safely hold a float value because their bit representations differ. Unions cannot store multiple values at once; only one field is reliably valid at a time. While reading wrong data may cause bugs, it typically does not crash the program.
Which key distinction sets unions apart from structs in C when examining memory allocation and member usage?
Explanation: Structs allocate space for all members, allowing them to store and access multiple values independently, whereas unions share a single memory location for all their members, allowing only one value at a time. The first option incorrectly reverses memory concepts. Unions do not provide dynamic resizing. The last option is incorrect because memory usage depends on the specific members, not just the type.
Which scenario best demonstrates an efficient use case for a union in a C program?
Explanation: Unions are useful when a variable may store different types at different times, such as error codes or sensor readings, but never both simultaneously. Employee records require all fields to be present at once, so a struct fits better. Arrays of strings and parallel arrays don't involve type switching or memory sharing, making them unsuitable for unions.
What is the result of initializing multiple members of a C union in a single statement, such as union Data { int i; float f; } info = { 10, 5.5f };?
Explanation: When initializing a union, only the first member listed in the declaration is set; subsequent initializers are ignored. The compiler does not automatically initialize the largest value, nor can it initialize multiple members because they overlap in memory. All members do not receive zeros unless explicitly set. Initializing both would require separate assignments, not a single initializer.