ProgrammingHardTCS NQT · Infosys pattern

What is printed?

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

Correct answer: D. 2 2

p[1] and *(p+1) are the same expression (a[2]=3); p[-1] and *(p-1) are also the same (a[0]=1). Both x and y compute 3-1=2.

More C Programming questions

Exams that ask this topic