What will be the output of the following C program?

int a[] = {10,20,30,40};
int *p = a;
printf("%d", *(p + 2));
                        

Options

  • A. 10
  • B. 20
  • C. 30
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. 30

Detailed Explanation

Correct Answer: 30

The array a contains 10, 20, 30 and 40. When p = a, the pointer points to the first element of the array.

p + 2 points to the third element because array indexing starts from 0. Therefore, *(p + 2) gives the value of the third element, which is 30.

So the output is 30.

Important: In C, a[i] is equivalent to *(a+i).