ProgrammingHardHCL · Tech Mahindra pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int a[4] = {1, 2, 3, 4};
int *p = a + 3;
int *q = a;
printf("%ld %d %d", (long)(p - q), *p, *(p - 2));
  1. A. 4 4 2
  2. B. 3 4 2
  3. C. 3 4 1
  4. D. 3 3 2

Correct answer: B. 3 4 2

Pointer subtraction p - q gives the number of ELEMENTS between them, not bytes: (a+3) - a = 3. *p dereferences a+3 = a[3] = 4. *(p - 2) is a[1] = 2. Output: 3 4 2.

More C Programming questions

Exams that ask this topic