ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[5];
    int *p = a + 4, *q = a + 1;
    printf("%ld", (long)(p - q) * (long)sizeof *p);
}
  1. A. 12
  2. B. 3
  3. C. 16
  4. D. 20

Correct answer: A. 12

Pointer subtraction gives 3 elements, and multiplying by sizeof(int) = 4 gives 12 bytes. Forgetting the element size gives 3.

More C Programming questions

Exams that ask this topic