The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+18 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);
    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.3k points)
edited by | 3.1k views
i understand ans will A
Compile and check, it comes out to be A
why d is printed 3 times ??/
    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.

Isn't  printf("%d",d); the ELSE part ?

if (n>1) count (n-1);   <-  If condition true 
    printf ("%d",d);    <- If condition false


Then C will be correct answer.

Isn't  printf("%d",d);  the ELSE part ?

if (n>1) count (n-1);   <-  If condition true 
    printf ("%d",d);    <- If condition false


Then C will be correct answer.

8 Answers

+30 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  \Rightarrow \ 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 (9.3k points)
edited by
is there two statements inside if statement ..... ?
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

+10 votes
(A)  3 1 2 2 1 3 4 4 4
answered by Junior (523 points)
pls compile and then check.its getting A. i too did wrong. i marked c in exam.
+8 votes

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 -

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 Loyal (3.7k points)
+3 votes
Answer is A.

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

Here is the running code of above question.
answered by (285 points)
+2 votes

ans is c (100% true)

answered by (95 points)
please compile and check it comes out to be A only
+2 votes
answer is A.bocoz d is static so it has one reference and finally d  value is printed
answered by (261 points)
+1 vote

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 Junior (941 points)
edited by
0 votes
Concept:- Static variable never reinitialize

and its lifetime is throughout the program.

Step1:- count(3)

print 3,1


Push d into Stack before jumping count(2)


print 2,2

d++; (d=3)

Push d into Stack before jumping 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 (23.4k 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...


Deepak singla

@ Rajesh Pradhan  

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

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

33,712 questions
40,255 answers
38,883 users