GATE CS 2025 Set 2 | Question 62

NAT · 2 marks

Consider the following C program:

#include <stdio.h>
int main(){
    int a;
    int arr[5] = {30,50,10};
    int *ptr;
    ptr = &arr[0] + 1;
    a = *ptr;
    (*ptr)++;
    ptr++;
    printf("%d", a + (*ptr) + arr[1]);
    return 0;
}

The output of the above program is ___________. (Answer in integer)

Answer

111

Explanation

  • The array holds {30, 50, 10, 0, 0}: elements without an initialiser become 0.
  • ptr = &arr[0] + 1 points to arr[1], so a = 50.
  • (*ptr)++ increments the element, not the pointer, so arr[1] becomes 51. The copy in a stays 50.
  • ptr++ moves the pointer to arr[2], so *ptr is 10.

The program prints \(50 + 10 + 51 = 111\).

Answer: 111