ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
union Data {
    int i;
    char c;
};
union Data d;
d.i = 0x12345678;
printf("%x", (unsigned char)d.c);
  1. A. 12
  2. B. 34
  3. C. 56
  4. D. 78

Correct answer: D. 78

On a little-endian machine (standard for x86/x86-64), the least-significant byte of 0x12345678 (0x78) is stored at the lowest address, which is exactly where the single-byte member c overlaps; reading d.c (cast to unsigned char for the %x format) gives 0x78.

More C Programming questions

Exams that ask this topic