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));- A. 4 4 2
- B. 3 4 2
- C. 3 4 1
- 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
- Assume a 64-bit LP64 ABI. What is printed? c void swap(int a, int b) { int tmp = a; a = b; b = tmp; } int main(void) { int x = 1, y = 2…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; p++; p += 10; printf("%d %d…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 100; int p = &x; int pp = &p; int ppp = &pp; ppp = 200; printf("%d %d", x, pp);
- Assume a 64-bit LP64 ABI. What happens when this runs? c int create(void) { int x = 42; return &x; } int main(void) { int p = create()…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = a; printf("%d ", p++); printf("%d ", ++p); printf("%d", p);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5; int p1 = &a; int p2 = &a; printf("%d ", p1 == p2); printf("%d", p1 == p2);