ProgrammingVery hardTCS NQT · Infosys

Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed?

Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed?
#include <stdio.h>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
int main(void) {
    printf("%zu %zu", sizeof(struct A), sizeof(struct B));
}
  1. A. 12 8
  2. B. 9 8
  3. C. 12 6
  4. D. 8 8

Correct answer: A. 12 8

Struct A pads after c and after d to reach 4-byte alignment, giving 12 bytes. Struct B fills its 4 bytes with i, then c and d fit in the next 2 bytes and the tail pads to 8.

More C Programming questions

Exams that ask this topic