ProgrammingHardIBM · Deloitte pattern

Assume a little-endian 64-bit machine. What is printed?

Assume a little-endian 64-bit machine. What is printed?
union U {
    int i;
    char bytes[4];
};
union U u;
u.i = 0x01020304;
printf("%x %x %x %x", (unsigned char)u.bytes[0], (unsigned char)u.bytes[1], (unsigned char)u.bytes[2], (unsigned char)u.bytes[3]);
  1. A. 1 2 3 4
  2. B. 2 1 4 3
  3. C. 0 1 2 3
  4. D. 4 3 2 1

Correct answer: D. 4 3 2 1

On a little-endian machine, the least significant byte is stored at the lowest address. 0x01020304's least significant byte is 0x04, so bytes[0]=0x04, bytes[1]=0x03, bytes[2]=0x02, bytes[3]=0x01 — giving 4 3 2 1 in hex.

More C Programming questions

Exams that ask this topic