edited by
15,592 views
44 votes
44 votes

 What will be the output of the following $C$ program?

void count (int n) {
    static int d=1;
    
    printf ("%d",n);
    printf ("%d",d);
    d++;
    if (n>1) count (n-1);
    printf ("%d",d);
    
    
}

void main(){
    count (3);
}
  1. $3 \ 1 \ 2 \ 2 \ 1 \ 3  \ 4 \ 4 \ 4$ 
  2.  $3 \ 1 \ 2  \ 1  \ 1 \ 1 \ 2 \ 2 \ 2$ 
  3.  $3 \ 1  \ 2 \ 2 \ 1 \ 3 \ 4$ 
  4.  $3 \ 1 \ 2  \ 1  \ 1  \ 1  \ 2$
edited by

11 Answers

0 votes
0 votes
D is the static variable so the answer is an option (A) 3 1 2 2 1 3 4 4 4

1. count(3): Print 3 1

     d=2,count(2)

2. count(2): print 2 2

     d=3,count(1)

3. count(1): print 3 1

Now the condition fails and it will print value 4, for 3 times.
0 votes
0 votes

Answer:  option (a) =>  312213444

first two prints for count (3) call
n=3 d=1

first two prints for count (2) call 
n=2 d=2 

first two prints for count (1) call
n=1 d=3

After that it will follow recursion stack to print remaining print statements.

Since 'd' is a static variable so value is shared among the recursive calls and it will print the latest updated value of d that is 3 times 4.. d=4 after evaluation of  d++ in last recursive call.

Answer:

Related questions