Project ForgeSign up free
Programming in C
Programming Fundamentals
GATE 2019
Expert

Consider the following C program:

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

10

Explanation

b points to a[4]=10. '*b - i' means (*b)-i = 10-i (dereference has higher precedence than subtraction). '*(b-i)' means a[4-i]. Term_i = (10-i) - a[4-i]: i=0: 10-a[4]=10-10=0; i=1: 9-a[3]=9-8=1; i=2: 8-a[2]=8-6=2; i=3: 7-a[1]=7-4=3; i=4: 6-a[0]=6-2=4. Sum=0+1+2+3+4=10.

Source: GATE CS 2019 (Set-2) (2019)

This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.

Start revising free