Skip to content
Related Articles

Related Articles

C | Storage Classes and Type Qualifiers | Question 11
  • Difficulty Level : Easy
  • Last Updated : 09 Feb, 2013

Consider the following C function




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

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

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.
My Personal Notes arrow_drop_up
Recommended Articles
Page :