ProgrammingVery hardTCS NQT · Infosys · Product Company SDE

What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> int main(void) { int a[] = {8, 21, 22, 25, 21, 40, 6, 33}; int *p = a + 3; printf("%d %d %d", *(p + 1) - *p, p[-1], (int)(&a[7] - p)); return 0; }

  1. A. 4 22 4
  2. B. 46 21 5
  3. C. -4 22 16
  4. D. -4 22 4

Correct answer: D. -4 22 4

p points at a[3]=25; *(p+1) is a[4]=21; p[-1] is a[2]=22; pointer subtraction counts elements, not bytes: 7−3 = 4.

More C Programming questions

Exams that ask this topic