ProgrammingVery hardProduct company OA · GATE CS

What is printed?

What is printed?
int inc(int x) { return x + 1; }
int dec(int x) { return x - 1; }
int (*pick(int c))(int) { return c ? inc : dec; }
int main(void) { printf("%d", pick(0)(pick(1)(pick(1)(10)))); }
  1. A. 11
  2. B. 12
  3. C. 9
  4. D. Compile error: a function cannot return a function pointer without a typedef

Correct answer: A. 11

pick(c) returns inc for nonzero c and dec for 0, so the chain is dec(inc(inc(10))) = 11; the declarator int (*pick(int c))(int) is valid without a typedef. 12 forgets the outer dec, and 9 swaps the ternary so that pick(0) gives inc.

More C Programming questions

Exams that ask this topic