ProgrammingHardHCL · Tech Mahindra pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;
int sum1 = arr[2] + *(arr + 2);
int sum2 = *(p + 2) + p[2];
printf("%d %d", sum1, sum2);
  1. A. 6 6
  2. B. 3 3
  3. C. 6 3
  4. D. 3 6

Correct answer: A. 6 6

arr[2] and *(arr + 2) are exactly equivalent notations for the same element, 3, so sum1 = 3+3 = 6. Likewise *(p + 2) and p[2] are equivalent too, giving sum2 = 3+3 = 6. Array indexing a[i] is always just syntactic sugar for *(a + i), regardless of whether a is an array name or a pointer variable.

More C Programming questions

Exams that ask this topic