ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

Correct answer: C. 2 5 5

p points at a[2]. p[-1] is a[1]=2. p[2] is a[4]=5. 2[p] is identical to p[2] by pointer-indexing commutativity (*(p+2) == *(2+p)) so it is also 5. Output: 2 5 5.

More C Programming questions

Exams that ask this topic