ProgrammingVery hardGATE CS

What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a multiple of its largest alignment.

What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a multiple of its largest alignment.
#include <stdio.h>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
struct C { char c; char d; short s; int i; };
int main(void) {
    printf("%zu %zu %zu", sizeof(struct A), sizeof(struct B), sizeof(struct C));
    return 0;
}
  1. A. 12 8 8
  2. B. 12 8 12
  3. C. 8 8 8
  4. D. 12 12 8

Correct answer: A. 12 8 8

struct A: c at 0, padding to 4, i at 4, d at 8, tail padding to 12. struct B: i at 0, c at 4, d at 5, padded to 8. struct C: c at 0, d at 1, s at 2, i at 4 gives 8 with no padding.

More C Programming questions

Exams that ask this topic