GATE 2019 CS – Question 63
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` is `a[4]=10` and `*(b-i)` is `a[4-i]`. Each term is `(*b - i) - *(b-i)` $=(10-i)-a[4-i]$:
| $i$ | $10-i$ | $a[4-i]$ | term |
|---|---|---|---|
| 0 | 10 | 10 | 0 |
| 1 | 9 | 8 | 1 |
| 2 | 8 | 6 | 2 |
| 3 | 7 | 4 | 3 |
| 4 | 6 | 2 | 4 |
Sum $=0+1+2+3+4=\mathbf{10}$.