ProgrammingVery hardTCS NQT · Accenture

What is printed? Assume short is 2 bytes with 2-byte alignment and char is 1 byte.

What is printed? Assume short is 2 bytes with 2-byte alignment and char is 1 byte.
#include <stdio.h>
struct S { short a; char b; };
int main(void) {
    struct S arr[3];
    printf("%ld", (long)((char *)&arr[2] - (char *)&arr[0]));
}
  1. A. 6
  2. B. 4
  3. C. 8
  4. D. 7

Correct answer: C. 8

The struct holds a 2-byte short and a 1-byte char, padded to a 2-byte multiple, so sizeof(struct S) = 4. Two elements apart is 8 bytes.

More C Programming questions

Exams that ask this topic