The GATE Grind

GATE 2019 CS – Question 63

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

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 ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 10

Explanation

$*b=10$ and $*(b-i)=a[4-i]$. The terms $(10-i)-a[4-i]$ are $0,1,2,3,4$ for $i=0,\dots,4$, so the sum is 10.