ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
int a[] = {1, 2, 3, 4, 5};
int *p = a + 1, *q = &a[4];
printf("%td %d %d", q - p, *p + *q, p[2]);
  1. A. 3 7 3
  2. B. 4 7 4
  3. C. 3 7 4
  4. D. 12 7 4

Correct answer: C. 3 7 4

Subtracting pointers into the same array gives the distance in elements, not bytes: 4 − 1 = 3 (12 would be the byte gap with 4-byte int). *p + *q = 2 + 5 = 7, and p[2] means *(p + 2) = a[3] = 4.

More C Programming questions

Exams that ask this topic