All mock tests are FREE till 31 December — register to save your progress

Consider the following C program:
#include<stdio.h>
int main() {
  int x[] = {2,4,6,8,10};
  int a, b = 0, *y = x + 4;
  for (a = 0; a < 5; a++) {
    b = b + (*y - a) - *(y - a);
  }
  printf("%d\n", b);
  return 0;
}
What will be the output of the above C program?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: D. 10

Explanation

*y = 10. For a = 0..4 the term (10 − a) − x[4 − a] is 10−10 = 0, 9−8 = 1, 8−6 = 2, 7−4 = 3, 6−2 = 4. Sum = 10.

More Programming Languages and Computer Graphics questions