C | Storage Classes and Type Qualifiers | Question 11

Consider the following C function

filter_none

edit
close

play_arrow

link
brightness_4
code

int f(int n) 
   static int i = 1; 
   if (n >= 5) 
      return n; 
   n = n+i; 
   i++; 
   return f(n); 
}

chevron_right


The value returned by f(1) is (GATE CS 2004)

(A) 5
(B) 6
(C) 7
(D) 8


Answer: (C)

Explanation: Since i is static, first line of f() is executed only once.



Execution of f(1)
    i = 1
    n = 2
    i = 2
 Call f(2)
    i = 2
    n = 4
    i = 3
 Call f(4)
   i = 3
   n = 7
   i = 4
 Call f(7)
  since n >= 5 return n(7)


Quiz of this Question



My Personal Notes arrow_drop_up