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 _______
GATE CSE 2019Programming in C › General C Programming ConceptsVerified
10
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.