ProgrammingVery hardProduct company OA

What is printed?

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

Correct answer: D. 20 -50 20

p[-2] is a[1] = 20. Postfix [] binds tighter than unary minus, so -1[p] is -(1[p]) = -(p[1]) = -a[4] = -50, not p[-1]. (p − 1)[-1] is a[1] = 20. '30' falls into exactly that trap.

More C Programming questions

Exams that ask this topic