ProgrammingHardTCS NQT · Infosys

char buf[10]; char *a = buf + 2, *b = buf + 9; What does printf("%td", b - a) print?

  1. A. 7
  2. B. 8
  3. C. 11
  4. D. 10

Correct answer: A. 7

Pointer subtraction gives the number of elements between the two positions, 9 - 2 = 7, with type ptrdiff_t (hence %td). 8 counts both endpoints inclusively, 11 adds the offsets 2 + 9, and 10 confuses the result with the array size.

More C Programming questions

Exams that ask this topic