ProgrammingVery hardTCS NQT · Infosys pattern

On a little-endian 64-bit machine, what is printed?

On a little-endian 64-bit machine, what is printed?
union U { int i; struct { short lo; short hi; } s; };
union U u;
u.i = 0x00020001;
printf("%d %d", u.s.lo, u.s.hi);
  1. A. 2 1
  2. B. 1 2
  3. C. 131073 0
  4. D. 0 131073

Correct answer: B. 1 2

On little-endian, the least-significant 16 bits of i occupy the lower address and alias lo, while the most-significant 16 bits alias hi. 0x00020001 has low 16 bits 0x0001 = 1 and high 16 bits 0x0002 = 2.

More C Programming questions

Exams that ask this topic