The GATE Grind

GATE 2025 CS (CS2) – Question 62

Programming and Data Structures · Programming in C · 2 marks · Numerical answer

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)

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 111

Explanation

Step by step:
- `arr[5] = {30, 50, 10, 0, 0}`.
- `ptr = &arr[0] + 1` points to `arr[1]` (value 50).
- `a = *ptr` gives `a = 50`.
- `(*ptr)++` increments `arr[1]` to 51.
- `ptr++` moves `ptr` to `arr[2]` (value 10).
- `printf("%d", a + (*ptr) + arr[1])` $=50+10+51=\mathbf{111}$.

More Programming in C questions

All Programming in C questions →