Skip to content
Official PYQ · 2025

GATE CS 2025 Set 2 Q52 — Programming in C

Explain with AI

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)

View worked solution

Answer: 111:111

Let us trace the execution of the program step-by-step:

  1. Array Initialization:
    int arr[5] = {30, 50, 10}; initializes the array elements as:
    arr[0] = 30, arr[1] = 50, arr[2] = 10, arr[3] = 0, and arr[4] = 0.
  2. Pointer Assignment:
    ptr = &arr[0] + 1; sets ptr to point to arr[1].
  3. Value Assignment:
    a = *ptr; assigns the value of arr[1] to a. Thus, a = 50.
  4. Increment Value:
    (*ptr)++; increments the value pointed to by ptr (which is arr[1]).
    So, arr[1] becomes 51.
  5. Increment Pointer:
    ptr++; advances ptr to point to the next element, arr[2].
  6. Print Statement:
    The expression evaluated is a + (*ptr) + arr[1].
    Substituting the values:
    Sum=50+10+51=111\text{Sum} = 50 + 10 + 51 = 111

Thus, the output of the program is 111.

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

Published
Report a correction
Keep exploringMore Programming in C questions