Project ForgeSign up free
Programming in C
Programming Fundamentals
GATE 2009
Hard

Consider the program below: `int fun(int n, int *f_p){ int t,f; if(n<=1){*f_p=1; return 1;} t=fun(n-1,f_p); f=t+*f_p; *f_p=t; return f;} int main(){ int x=15; printf("%d\n", fun(5,&x)); return 0;}`. The value printed is:

  • A6
  • 8
  • C14
  • D15

Explanation

Tracing with x aliased via f_p throughout: fun(1) sets x=1, returns1. fun(2): t=1,f=1+x(1)=2,x set to1,returns2. fun(3): t=2,f=2+x(1)=3,x set to2,returns3. fun(4): t=3,f=3+x(2)=5,x set to3,returns5. fun(5): t=5,f=5+x(3)=8,returns8.

Source: GATE CS 2009 (2009)

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