ProgrammingVery hardGATE CS · TCS NQT

What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in place?

What is the sizeof of this struct, assuming char has size 1, int has size 4 with 4-byte alignment, and a nested struct is laid out in place?
#include <stdio.h>
struct In { int i; char d; };
struct Out { char c; struct In in; };
int main(void) {
    printf("%zu", sizeof(struct Out));
}
  1. A. 12
  2. B. 10
  3. C. 9
  4. D. 16

Correct answer: A. 12

struct In is 8 bytes after padding, and it must start at offset 4 to be int-aligned after the char c. The outer struct is 4 + 8 = 12 bytes.

More C Programming questions

Exams that ask this topic