Question:

What will be the output of the following C code?

# include <stdio.h>
int main()
{ int arr[5] = {1, 2, 3, 4, 5};
 int *ptr = arr;
  *(ptr + 2) = *(ptr + 4) + *(ptr + 1);
  printf("%d\n", arr[2]);
  return 0; }

Show Hint

Rewrite each *(ptr + k) as arr[k] and compute.
Updated On: Jul 2, 2026
  • 3
  • 5
  • 7
  • 9
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

Step 1: Set up the array. arr holds {1, 2, 3, 4, 5} at indices 0 to 4, and ptr points to arr[0].

Step 2: Read the right side of the assignment. *(ptr + 4) is arr[4] which is 5. *(ptr + 1) is arr[1] which is 2.

Step 3: Add them. \(5 + 2 = 7\).

Step 4: The left side *(ptr + 2) is arr[2]. So arr[2] is now set to 7.

Step 5: printf prints arr[2], which is 7. The answer is 7 (option C).
Was this answer helpful?
0
0