ProgrammingHardProduct company OA · GATE CS

Assume 4-byte int. What is printed?

Assume 4-byte int. What is printed?
struct Msg { int len; char data[]; };
struct Msg *m = malloc(sizeof *m + 10);
printf("%zu", sizeof *m);
  1. A. 14
  2. B. 8
  3. C. 5
  4. D. 4

Correct answer: D. 4

data[] is a C99 flexible array member: it adds no size of its own, so sizeof(struct Msg) is just the int (4 here). The extra 10 bytes from malloc are what data uses, but sizeof is fixed at compile time and never sees them.

More C Programming questions

Exams that ask this topic