Difficulty: Medium
Correct Answer: Error: in initializing z2
Explanation:
Introduction / Context:
This question targets understanding of how unions are initialized in C and what is allowed in a brace-enclosed initializer for a union when no designated initializers are used.
Given Data / Assumptions:
Concept / Approach:
When initializing a union without designators, only the first member may be initialized. The initializer corresponds to that first member’s type. For union a, the first member is int i. Therefore a single value that can initialize i is valid. Supplying multiple values as if initializing an array or struct is invalid for a union without designators.
Step-by-Step Solution:
Verification / Alternative check:
A correct way to initialize the char array member would be to make it the first member or to use a designated initializer if supported (e.g., .ch = { 0, 2 }).
Why Other Options Are Wrong:
Common Pitfalls:
Confusing struct and union initialization rules, and forgetting that only one member exists at a time in a union, with the initializer mapping to the first member unless designated otherwise.
Final Answer:
Error: in initializing z2
Discussion & Comments