ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int main(void) {
    int (*ops[])(int, int) = {add, sub, mul};
    int r = 10;
    for (int i = 0; i < 3; i++)
        r = ops[i](r, i + 2);
    printf("%d", r);
}
  1. A. 28
  2. B. 33
  3. C. 36
  4. D. 40

Correct answer: C. 36

The steps are 10 + 2 = 12, then 12 - 3 = 9, then 9 * 4 = 36. Applying the operator with the wrong argument index gives the other values.

More C Programming questions

Exams that ask this topic