ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int a[3] = {1,2,3};
int *p = &a[1];
int x = p[1] - p[-1];
int y = *(p+1) - *(p-1);
printf("%d %d", x, y);- A. 1 1
- B. 2 1
- C. 3 2
- D. 2 2
Correct answer: D. 2 2
p[1] and *(p+1) are the same expression (a[2]=3); p[-1] and *(p-1) are also the same (a[0]=1). Both x and y compute 3-1=2.
More C Programming questions
- What is printed? c int add(int a,int b){ return a+b; } int sub(int a,int b){ return a-b; } int mul(int a,int b){ return ab; } int…
- What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously. c int a[2][3] = {{1,2,3},{4,5,6}}; int…
- What is printed? c int p = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++) p[i] = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++)…
- What is printed? c int r0[] = {1,2,3}; int r1[] = {4,5}; int r2[] = {6,7,8,9}; int jag[3] = {r0, r1, r2}; int lens[3] = {3,2,4}; int sum =…
- What is printed? c int a[4] = {10,20,30,40}; int arr[4]; for (int i = 0; i < 4; i++) arr[i] = &a[i]; int pp = arr; pp += 2; printf("%d…
- Which declaration correctly declares p as an array of 10 pointers to functions taking no arguments and returning a pointer to int?