Skip to main content

GATE CSE 2025Programming in C › Pointers, Arrays & StringsVerified

GATE CSE 2025 PYQ: Consider the following C program: #include <stdio.h> int main (){ int a ; int arr [ 5 ] = { 30 , 50 , 10 };…

Consider the following C program:

  1. #include<stdio.h>
  2. int main(){
  3. int a;
  4. int arr[5] = {30,50,10};
  5. int *ptr;
  6. ptr = &arr[0] + 1;
  7. a = *ptr;
  8. (*ptr)++;
  9. ptr++;
  10. printf("%d", a + (*ptr) + arr[1]);
  11. return 0;
  12. }


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

    Answer

    111

    Approach

    Simulate the program statement by statement, tracking the value stored in each array element and the address that ptr holds at every step. The key idea is that (*ptr)++ modifies the array element itself, whereas ptr++ only moves the pointer, so arr[1] is read after it has already been incremented.

    After all pointer operations, a = 50, *ptr = arr[2] = 10, and arr[1] = 51. Their sum is 50 + 10 + 51 = 111, so the program prints 111.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.