GATE CSE
First time here? Checkout the FAQ!
x
+10 votes
2k views

 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$

 

 

asked in Programming by Boss (8.9k points)   | 2k views
i understand ans will A
Compile and check, it comes out to be A
why d is printed 3 times ??/
if
{
count(n-1);
    printf("%d ", d);
}

both statements are inside if , so for each count func d will b printed which is 4 finally....
so a option correct
Could anyone solve the problem with the help of recursion tree as I got stuck at one place while solving ?

7 Answers

+23 votes
Best answer

Here d is Static, so the value of d will persists between the function calls.

  1.  count(3) will print the value of n and d and increments d and call count(2)  = prints 3 1.
  2.  count(2) will print the value of n and d and increments d and call count(1)  = prints 2 2.
  3.  count(1) will print the value of n and d and increments d  => prints 1 3.

Now it will return and prints the final incremented value of d which is 4, 3 times.
So, option A is correct = 3 1 2 2 1 3 4 4 4
 

answered by Boss (8.2k points)  
edited by
is there two statements inside if statement ..... ?
no
why the value of d is printed 3 times ?
bcz for count(1) print 4 first time then goes  count (2) then print 4 then goes count(3) then print 3 thats why d values print 3 times
i was soooooooo stupid to  miss this easy question in 2016 :(
In case of printing three times '4' , I have a doubt.
If activation record stores 2,3,4 in that order, then it should print 4,3,2 at last.
And static keyword says initialization of a variable should be one time. It is not told in static that last activation record value will print every time. So, why three times '4' will be printed? What is memory allocation for static?
+8 votes
(A)  3 1 2 2 1 3 4 4 4
answered by Junior (519 points)  
Yes c is right option
pls compile and then check.its getting A. i too did wrong. i marked c in exam.
+2 votes
Answer is A.

When the recursion ends. 4 will be printed 3 times.

Here is the running code of above question.

https://ideone.com/JDW1Py
answered by (273 points)  
+1 vote

ans is c (100% true)

answered by (71 points)  
please compile and check it comes out to be A only
+1 vote
answer is A.bocoz d is static so it has one reference and finally d  value is printed
answered by (79 points)  
0 votes
Concept:- Static variable never reinitialize

and its lifetime is throughout the program.

Step1:- count(3)

print 3,1

d++;(d=2)

Push d into Stack before jumping count(2)

Step2:-count(2)

print 2,2

d++; (d=3)

Push d into Stack before jumping count(1)

Step3:-Count(1)

Print 1,3

d++; (d=4)

Print d means 4

Now d have been pushed two times into stack now they will pop and print 4 two times

Here d is static variable so latest value will be printed while poping if it is auto variable then we must have to print the value of d at the time we pushed it.

So finally the o/p is

Option A. 3 1 2 2 1 3 4 4 4
answered by Veteran (17k points)  
i did this in the same way ....well done bro......

 

when the value of d is incremented nd the fucntion call count(2) is made then the statment written after recursion (printf("%d",d);) is PUSH onto the stack twice....and when the condition failed in if condition then the last value of d=4 is printed and then the 2 elemnets which are pused on to the stack is poped out one by one and print the vale of d twice...

Thnks

Deepak singla
0 votes

count(3): 3  1   d=2   count(2): 2  2   d=3   count(1): 1  3   d=4      (now print final value of d  3 times)   4  4  4

finally: 3 1  2  2  1  3  4  4  4

why: however the previous values of d(in activation stack) are 2, 3 and 4 but last three values of d will print only 4  4  4 beacause d is static and all other values are overwritten by 4 finally in last count().

answered by (365 points)  
edited by


Top Users Jul 2017
  1. Bikram

    3782 Points

  2. manu00x

    2464 Points

  3. Debashish Deka

    1832 Points

  4. joshi_nitish

    1494 Points

  5. Arnab Bhadra

    1096 Points

  6. Arjun

    1054 Points

  7. Hemant Parihar

    1050 Points

  8. Shubhanshu

    972 Points

  9. Ahwan

    876 Points

  10. akash.dinkar12

    642 Points


23,953 questions
30,895 answers
70,108 comments
29,272 users