Difficulty: Medium
Correct Answer: 3, 2, 515
Explanation:
Introduction / Context: This question tests understanding of unions, byte ordering (endianness), and how writing to a char array inside a union affects the overlapping int member in C.
Given Data / Assumptions:
Concept / Approach: In a union, all members share the same memory. On little-endian systems, the least significant byte is stored at the lowest address. With a 16-bit int, ch[0] maps to the low byte of i and ch[1] maps to the high byte of i. Therefore i = ch[0] + ch[1]256 = 3 + 2256 = 515.
Step-by-Step Solution:
Write ch[0] = 3 → low byte of i becomes 3.Write ch[1] = 2 → high byte of i becomes 2.Compute i = 2256 + 3 = 512 + 3 = 515.printf prints: 3, 2, 515.Verification / Alternative check: If int is 32-bit little-endian, the low two bytes would still be 03 and 02, giving i = 515 with higher bytes 0. Hence the printed value for i remains 515 on common little-endian systems.
Why Other Options Are Wrong:
Common Pitfalls: Confusing endianness (big-endian would differ), assuming unions copy rather than overlay, or assuming chars sign-extend into the int. Here bytes are combined by position, not sign extension.
Final Answer: 3, 2, 515
Discussion & Comments