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);- A. 1 2 3 4 5 | 20
- B. 2 3 4 5 1 | 5
- C. 1 2 3 4 5 | 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
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5; int p = &a; void vp = p; int p2 = (int )vp; p2 = 99; printf("%d %d", a, p);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = a; int q = &a[2]; ptrdifft diff = q - p; printf("%td %d", diff…
- Assume a 64-bit LP64 ABI. What is printed? c int rows = 2, cols = 3; int matrix = malloc(rows sizeof(int )); for (int i = 0; i < rows…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; p += 5; p = p; p = 2; printf("%d", a);
- Assume a 64-bit LP64 ABI. What is printed? c int a[2][2] = {{1, 2}, {3, 4}}; int p = a[0]; int q = a[1]; printf("%ld", q - p);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1; int b = 2; int parr[2]; parr[0] = &a; parr[1] = &b; int pp = parr; for (int i = 0…