Skip to content
Official PYQ · 2019

GATE CS 2019 Q24 — Programming in C

Explain with AI

Consider the following C program:

#include <stdio.h>
int main() {
        int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4;
        printf(“%d\n”, ip[1]);
        return 0;
}

The number that will be displayed on execution of the program is _______

View worked solution

Answer: 6

To find the output of the given C program, let's trace the array elements and pointer arithmetic step-by-step:

  1. Array Initialization: The array arr is initialized with the following elements and their corresponding 0-based indices:
    Index 0 1 2 3 4 5 6 7 8 9 10 11 12
    Value 1 2 3 4 5 6 7 8 9 0 1 2 5
  2. Pointer Initialization: The pointer ip is initialized as:
    int *ip = arr + 4;
    In C, the array name arr decays to a pointer to its first element (&arr[0]). Therefore, arr + 4 points to the element at index 4, which is arr[4] (value 5).
  3. Evaluating the Print Statement: The program prints ip[1]. Using the definition of array subscripting in C: ip[1]=∗(ip+1)ip[1] = *(ip + 1) Substituting ip = arr + 4 into the equation: ip[1]=∗((arr+4)+1)=∗(arr+5)=arr[5]ip[1] = *((arr + 4) + 1) = *(arr + 5) = arr[5] The value stored at arr[5] is 6.

Thus, the number displayed on execution of the program is 6.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions