ProgrammingHardTCS NQT · Infosys

What is printed? Assume short is 2 bytes, int is 4 bytes and both use natural alignment.

What is printed? Assume short is 2 bytes, int is 4 bytes and both use natural alignment.
#include <stdio.h>
struct S { char c; short s; int i; };
int main(void) {
    struct S arr[3];
    printf("%ld", (long)((char *)&arr[2] - (char *)&arr[0]));
}
  1. A. 8
  2. B. 16
  3. C. 12
  4. D. 10

Correct answer: B. 16

struct S has 1 + 1 + 2 + 4 bytes of members plus padding that makes its size 8, so arr[2] lies 2 * 8 = 16 bytes past arr[0]. Using the unpadded size of 7 would give 14.

More C Programming questions

Exams that ask this topic