ProgrammingHardTCS NQT · Infosys pattern

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

Assume a 64-bit LP64 ABI. What is printed?
typedef union {
    struct { unsigned char r, g, b, a; } channels;
    unsigned int packed;
} Color;
Color c;
c.channels.r = 255;
c.channels.g = 0;
c.channels.b = 0;
c.channels.a = 255;
printf("%zu", sizeof(Color));
  1. A. 4
  2. B. 16
  3. C. 8
  4. D. 1

Correct answer: A. 4

The union's two members are the anonymous struct (4 unsigned chars, 4 bytes total, no padding needed since all members are 1-byte-aligned) and packed (a 4-byte unsigned int). Both members are 4 bytes, so the union itself is 4 bytes — a classic technique for viewing 4 packed color channels as one 32-bit integer.

More C Programming questions

Exams that ask this topic