ProgrammingHardIBM · Deloitte pattern

On a little-endian machine with 4-byte int and ASCII, what is printed?

On a little-endian machine with 4-byte int and ASCII, what is printed?
union { int i; char c; } u;
u.i = 0x41424344;
printf("%c", u.c);
  1. A. A
  2. B. D
  3. C. A garbage character
  4. D. B

Correct answer: B. D

All union members start at the same address, so u.c reads the first byte of i. Little-endian stores the least significant byte first, 0x44, which is 'D'. A big-endian machine would print 'A'.

More C Programming questions

Exams that ask this topic