ProgrammingVery hardIBM · Deloitte pattern

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

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

Correct answer: B. 5 20

q - p as int pointers counts elements: 5. Casting both to char* before subtracting counts bytes: 5 * sizeof(int) = 20 on a 4-byte-int ABI. The two subtractions scale differently because pointer arithmetic divides the raw byte difference by the pointee's size.

More C Programming questions

Exams that ask this topic