ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
/* 4-byte int with 4-byte alignment */
struct S { int a; char b; } arr[5];
printf("%td %td", &arr[4] - &arr[1],
                  (char *)&arr[4] - (char *)&arr[1]);
  1. A. 3 15
  2. B. 24 24
  3. C. 3 3
  4. D. 3 24

Correct answer: D. 3 24

Subtracting two struct S pointers gives the distance in ELEMENTS: 4 − 1 = 3. Each struct S is padded from 5 bytes to 8 so that the int in the next array element stays 4-byte aligned, so the same distance measured with char pointers is 3 × 8 = 24 bytes. 15 assumes no tail padding.

More C Programming questions

Exams that ask this topic