GATE CSE
First time here? Checkout the FAQ!
x
+10 votes
1.7k 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)   | 1.7k 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

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 (7.7k points)  
selected 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 :(
+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 (61 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 (16.3k 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 (361 points)  
edited by


Top Users Mar 2017
  1. rude

    5236 Points

  2. sh!va

    3054 Points

  3. Rahul Jain25

    2920 Points

  4. Kapil

    2732 Points

  5. Debashish Deka

    2602 Points

  6. 2018

    1574 Points

  7. Vignesh Sekar

    1430 Points

  8. Bikram

    1424 Points

  9. Akriti sood

    1420 Points

  10. Sanjay Sharma

    1128 Points

Monthly Topper: Rs. 500 gift card

21,549 questions
26,889 answers
61,248 comments
23,251 users