ProgrammingVery hardTCS NQT · Infosys · Product Company SDE
What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> int main(void) { int a[] = {15, 38, 22, 5, 30, 11, 38, 22}; int *p = a + 3; printf("%d %d %d", *(p + 1) - *p, p[-1], (int)(&a[7] - p)); return 0; }
- A. 35 38 5
- B. 25 22 16
- C. 25 22 4
- D. -25 22 4
Correct answer: C. 25 22 4
p points at a[3]=5; *(p+1) is a[4]=30; p[-1] is a[2]=22; pointer subtraction counts elements, not bytes: 7−3 = 4.
More C Programming questions
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> int…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> int…
- Assume a 64-bit LP64 ABI. What is printed? c int a[4] = {1,2,3,4}; int (p)[4] = &a; printf("%d %d %zu", (p)[2], p, sizeof(p));
- What is printed? c int a[] = {1,2,3,4,5}; int p = a + 3; int q = a; printf("%d %d %d", p - q, p-- , p);
- What is printed? c int a[5] = {1,2,3,4,5}; int p = &a[2]; printf("%d %d %d", p[-1], p[2], 2[p]);
- Assume a 64-bit LP64 ABI. What is printed? c int a[10]; int p = a + 2; int q = a + 7; printf("%ld %ld", (long)(q - p), (long)((char)q…