edited by
14,234 views
28 votes
28 votes

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:

  1. $5$
  2. $6$
  3. $7$
  4. $8$
edited by

3 Answers

Best answer
38 votes
38 votes
Answer is $7$. As,

$f(1):n=2,i=2$

$f(2):n=4,i=3$

$f(4):n=7,i=4$

$f(7):print(n) \Rightarrow 7$ <ans>
edited by
16 votes
16 votes
Static storage class allows to initialize a variable for one time and re-initialization can't be done till end of program .

Here Iteration1: n(1)<=5; and  i=1;

                        n(2)=n(1)+i(1);

                        i(2)=i(1)+1;

Iteration2: n(2)<=5; and  i=2;

                        n(4)=n(2)+i(2);

                        i(3)=i(2)+1;

Iteration3: n(4)<=5; and  i=3;

                        n(7)=n(4)+i(3);

                        i(4)=i(3)+1;

Iteration2: n(7)<=5; and program execution terminates . So 7 is returned.
Answer:

Related questions

34 votes
34 votes
2 answers
1
Kathleen asked Sep 18, 2014
12,791 views
Consider the following C program segment:char p[20]; int i; char* s = "string"; int length = strlen(s); for(i = 0; i < length; i++) p[i] = s[length-i]; printf("%s", p);Th...
24 votes
24 votes
2 answers
2
Kathleen asked Sep 18, 2014
8,891 views
The goal of structured programming is to:have well indented programsbe able to infer the flow of control from the compiled codebe able to infer the flow of control from t...
2 votes
2 votes
2 answers
3
go_editor asked Jun 13, 2016
4,277 views
In C, what is the effect of a negative number in a field width specifier?the values are displayed right justifiedthe values are displayed centeredthe values are displayed...
6 votes
6 votes
1 answer
4
go_editor asked Jun 13, 2016
3,169 views
What is the value of $F(4)$ using the following procedure:function F(K : integer) integer; begin if (k<3) then F:=k else F:=F(k-1)*F(k-2)+F(k-3) end;$5$$6$$7$$8$