Unions in C: Memory-Saving Data Structures Quiz Quiz

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.

  1. Union Memory Usage

    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?

    1. 13 bytes
    2. 4 bytes
    3. 16 bytes
    4. 8 bytes

    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.

  2. Union Value Overwriting

    If you assign a value to the float member of a union and then immediately read the int member, what is likely to happen?

    1. The int member shows an undefined or garbage value
    2. The program crashes due to memory overlap
    3. The union stores both values simultaneously
    4. The int member holds the float value safely

    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.

  3. Difference Between Struct and Union

    Which key distinction sets unions apart from structs in C when examining memory allocation and member usage?

    1. Unions allow dynamic member resizing, but structs do not
    2. Unions allocate memory equal to the sum of all members, while structs allocate for only the largest member
    3. Structs can store multiple values simultaneously, but unions can store only one at a time in shared memory
    4. Structs always require more memory than unions, regardless of member types

    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.

  4. Practical Application of Unions

    Which scenario best demonstrates an efficient use case for a union in a C program?

    1. Managing parallel arrays for multiple unrelated data types
    2. Storing a set of employee records where each record holds ID, name, and salary together
    3. Keeping a static array of fixed-length strings
    4. Creating a data type that switches between integer error codes and floating-point sensor readings in a single variable

    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.

  5. Union Initialization Behavior

    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 };?

    1. The compiler automatically picks the largest value
    2. Both members are correctly initialized in memory
    3. Only the first member is initialized; others are ignored
    4. All members receive default zero values

    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.