GATE CSE
First time here? Checkout the FAQ!
x
+12 votes
2.3k 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 (9.1k points)   | 2.3k 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 ?

Hey @astha u r wrong.

8 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.5k 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?
The third call count(1) will print 1 3 4 and will return to count(2) which will print another 4. Again, will return to count(3) and will print another 4.

static means there is one version of the variable. Each time a function is called that contains a static variable the variable will have the last value assigned to it. hence 4 would be printed 3 times

+8 votes
(A)  3 1 2 2 1 3 4 4 4
answered by Junior (529 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 (79 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)  
+1 vote

A static local variable is different from a local variable as a static local variable is initialized only once no matter how many times the function in which it resides is called and its value is retained and accessible through many calls to the function in which it is declared.

If a variable is declared static in a function, the same variable/same copy will be used for all recursive calls of that function.

Static variables are allocated memory in the data segment, not stack segment. In this case, d is initialized explicitly so it will be stored in initialized data segment.

Check this - http://www.geeksforgeeks.org/memory-layout-of-c-program/

here count(3) will do 3 things  - prints 31, increments d and call count(2)

count(2) - print 22, increments d and call count(1)

count(1) -  print 13, increments d. Now as 0 $\ngtr 1$ , of it will print final value of d i.e 4 and returns to count(2). count(2) will print 4 and returns to count(3), At last count(3) will print 4.

Final output - 312213444

A is the correct answer. 

answered by Active (1.6k points)  
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 (401 points)  
edited by
–1 vote
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 (18.6k 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

@ Rajesh Pradhan  

u missed out this " Static variables are allocated memory in data segment, not stack segment ".



Top Users Sep 2017
  1. Habibkhan

    7142 Points

  2. Warrior

    2640 Points

  3. Arjun

    2480 Points

  4. rishu_darkshadow

    2466 Points

  5. A_i_$_h

    2214 Points

  6. nikunj

    1980 Points

  7. manu00x

    1846 Points

  8. makhdoom ghaya

    1770 Points

  9. Bikram

    1744 Points

  10. SiddharthMahapatra

    1718 Points


26,133 questions
33,705 answers
79,886 comments
31,105 users