Skip to main content

GATE CSE 2019Programming in C › General C Programming ConceptsVerified

GATE CSE 2019 PYQ: Consider the following C program: #include <stdio.h> int main () { int a [] = { 2 , 4 , 6 , 8 , 10 }; int i…

Consider the following C program:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int a[] = {2, 4, 6, 8, 10};
  5. int i, sum=0, *b=a+4;
  6. for (i=0; i<5; i++)
  7. sum=sum+(*b-i)-*(b-i);
  8. printf("%d\n", sum);
  9. return 0;
  10. }

The output of the above C program is _______

    Answer

    10

    Approach

    Track the pointer b and evaluate the loop body step by step. b is initialized to a + 4, so it permanently points at the last element a[4] = 10 and is never modified. Since *(b - i) is just a[4 - i], each iteration adds (10 - i) and subtracts one array element, walking the array backwards.

    The loop accumulates sum = $Σ_{i=0}^{4}$ [(10 - i) - a[4 - i]], which evaluates to 10. The correct numeric output is 10.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.