ProgrammingVery hardHCL · Tech Mahindra pattern

Assume enum's underlying type is int (typical on common compilers) and a 64-bit LP64 ABI. What is printed?

Assume enum's underlying type is int (typical on common compilers) and a 64-bit LP64 ABI. What is printed?
enum Color { RED, GREEN, BLUE };
struct S { enum Color c; char tag; };
printf("%zu", sizeof(struct S));
  1. A. 8
  2. B. 5
  3. C. 4
  4. D. 12

Correct answer: A. 8

c is stored as a 4-byte int-sized enum at offset 0; tag@4 (1 byte); end 5 is padded up to the struct's alignment, 4 (from the enum/int), giving 8.

More C Programming questions

Exams that ask this topic