ProgrammingHardWipro · Cognizant pattern

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

Assume a 64-bit LP64 ABI. What is printed?
int nums[] = {1, 2, 3, 4, 5};
int *p = nums;
for (int i = 0; i < 5; i++) {
    printf("%d ", *p);
    p = p + 1;
}
printf("| %ld", p - nums);
  1. A. 1 2 3 4 5 | 20
  2. B. 2 3 4 5 1 | 5
  3. C. 1 2 3 4 5 | 4
  4. D. 1 2 3 4 5 | 5

Correct answer: D. 1 2 3 4 5 | 5

The loop prints each element in order by advancing p one int at a time (five iterations: 1 2 3 4 5). After the loop p has been advanced 5 times from nums, so p - nums (pointer difference, in element units, not bytes) equals 5.

More C Programming questions

Exams that ask this topic