Skip to content
Official PYQ · 2019

GATE CS 2019 Q53 — Programming in C

Explain with AI

Consider the following C program:

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

The output of the above C program is _______

View worked solution

Answer: 10

The array a is initialized as {2, 4, 6, 8, 10}. The pointer b is initialized to a + 4, which points to the last element a[4]. Thus, *b = 10 remains constant throughout the execution.

The loop runs for i=0i = 0 to 44. In each iteration, the sum is updated as:

sum=sum+(∗b−i)−∗(b−i)\text{sum} = \text{sum} + (*b - i) - *(b - i)

Let's trace each iteration step-by-step:

  • i=0i = 0: sum=0+(10−0)−a[4]=0+10−10=0\text{sum} = 0 + (10 - 0) - a[4] = 0 + 10 - 10 = 0
  • i=1i = 1: sum=0+(10−1)−a[3]=0+9−8=1\text{sum} = 0 + (10 - 1) - a[3] = 0 + 9 - 8 = 1
  • i=2i = 2: sum=1+(10−2)−a[2]=1+8−6=3\text{sum} = 1 + (10 - 2) - a[2] = 1 + 8 - 6 = 3
  • i=3i = 3: sum=3+(10−3)−a[1]=3+7−4=6\text{sum} = 3 + (10 - 3) - a[1] = 3 + 7 - 4 = 6
  • i=4i = 4: sum=6+(10−4)−a[0]=6+6−2=10\text{sum} = 6 + (10 - 4) - a[0] = 6 + 6 - 2 = 10

Therefore, the final output printed by the program is 10.

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

Published
Report a correction
Keep exploringMore Programming in C questions