3
#include <stdio.h>
void count(int );

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

int main(void) {
    count(3);
    return 0;
}

Output : 3 1 2 1 1 1 2 2 2


#include <stdio.h>
void count(int );

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

int main(void) {
    count(3);
    return 0;
}

Output : 3 1 2 2 1 3 4 4 4


In both the outputs, I am unable to get the difference between last 3 digits in the output, i.e, 444 and 222.

I know that static declaration has static allocation at compile time, so its value is retained throughout the program and local/auto variable is destroyed as soon as function call ends.

But, even though I am confused in the last 3 digits only as I can't interpret them based on the above information ?

Aldrich Taylor
  • 41
  • 1
  • 1
  • 7
  • Well for such kind of Questions u need to plot a diagram accordingly with the call of function.since it will be recursion so value at the particular instance in the past might be different and you are jumbling that up..go with the flow of diagram..similar question is standard question asked in GATE exam india :) – minigeek Feb 04 '17 at 15:32
  • Do you know what does `static` storage type mean ? – Meninx - メネンックス Feb 04 '17 at 15:33
  • 2
    @Meninx-メネンックス His confusion isn't about static, it seems to be about the order of printing when he recurses. – Barmar Feb 04 '17 at 15:33
  • 2
    When you don't understand how a program like this is working, try to execute it by hand on paper. – Barmar Feb 04 '17 at 15:35
  • @Barmar Thanks !! You got my doubt. Actually by hand, I am getting 432. Just clear this :-) –  Feb 04 '17 at 15:37
  • You must not be doing everything in the recursive call, including additional calls, before executing the statement after it. – Barmar Feb 04 '17 at 15:38
  • @minigeek Yeahh :-) –  Feb 04 '17 at 15:41

5 Answers5

3

The printf() at the end of your function will be run for the count(n=1) case first. Once that returns, the printf() runs for count(n=2), then for count(n=3).

In the non-static case, the local variable d is always 2 at the end of the function. In the static case, all share the same variable, so it will be 4 after the last count() has incremented it.

Try something like this so you can better see what happens:

void count(int n)
{
    static int d = 1;
    d++;
    printf("%d: pre recursion (d = %d)\n", n, d);
    if(n > 1) count(n-1);
    printf("%d: post recursion (d = %d)\n", n, d);
} 
puzzle
  • 6,071
  • 1
  • 25
  • 30
  • "In the static case, all share the same variable". Can you elaborate more this line. Sorry, I am new to this .. –  Feb 04 '17 at 15:40
  • The `static` keyword here means that `d` is created and initialized just once over the lifetime of the program, then reused every time `count()` is called. So in the end, after three executions of `d++`, it will be 4. And since that `printf()` happens after that, you'll end up with 4 being printed three times. You can find more info, also on other ways `static` is used, in [this question](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program). – puzzle Feb 04 '17 at 17:11
3

When you make a recursive call, the statements after the call are not executed until the recursion ends. This includes all the nested recursive calls. Here's what happens when you call the version with the automatic variable:

  • main() calls count(3)
    • Initializes d = 1
    • prints n, which contains 3
    • prints d, which contains 1
    • increments d, it now contains 2
    • calls count(n-1)
      • initializes d = 1
      • prints n, which contains 2
      • prints d, which contains 1
      • increments d, it now contains 2
      • calls count(n-1)
        • initializes d = 1
        • prints n, which contains 1
        • prints d, which contains 1
        • increments d, it now contains 2
        • if (n > 1) fails, so it doesn't recurse
        • prints d, which contains 2
        • returns
      • prints d, which contains 2
      • returns
    • prints d, which contains 2
    • returns

If you find all the print lines above, it prints 3 1 2 1 1 1 2 2 2

Here's what happens when you use the version with the static variable:

  • main() calls count(3)
    • Initializes d = 1 because this is the first call to the function
    • prints n, which contains 3
    • prints d, which contains 1
    • increments d, it now contains 2
    • calls count(n-1)
      • prints n, which contains 2
      • prints d, which contains 2
      • increments d, it now contains 3
      • calls count(n-1)
        • prints n, which contains 1
        • prints d, which contains 3
        • increments d, it now contains 4
        • if (n > 1) fails, so it doesn't recurse
        • prints d, which contains 4
        • returns
      • prints d, which contains 4
      • returns
    • prints d, which contains 4
    • returns

Extract all the print lines, and it prints 3 1 2 2 1 3 4 4 4.

The difference is that d keeps getting higher each time the function is called, because it retains its value from the previous call and then it gets incremented.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

The last three digits are the value of d, as visible, after any and all recursive calls to count have completed.

In the first program, d is scoped to each individual call to count, and it only ever increases by 1 in each stack frame. 1 + 1 = 2, and the function prints 2 before exiting.

In the second program, d is static to the program, and shared between every call to count. As mentioned earlier, the final print statement only occurs after all calls to count have run, and there will be no more calls to count, so d will always be at its maximum value here - no matter which call to count is finishing up.

Oka
  • 23,367
  • 6
  • 42
  • 53
0

In this function

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

the local variable d has static storage duration it keeps its value between function calls. It is initialized only once before the program startups.

So in this code snippet

    if(n > 1) count(n-1);
    printf("%d ", d);

the last statement is executed when in the inner-most call the variable d already gets its value 4.

You can imagine it the following way

d++;
d++;
d++;
//...

printf("%d ", d);
printf("%d ", d);
printf("%d ", d);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    He understands this, he wrote **I know that static declaration has static allocation at compile time, so its value is retained throughout the program** – Barmar Feb 04 '17 at 15:34
0

In first program d is auto variable, So, each time function is executed memory for d is reserved and after completion of function it is unreserved.

In second program, d is static variable, So, first time execution of function d variable memory is reserved but after compilation of function it is not unreserved. static variable memory will be unreserved only when the whole program will completed.

Ishvar Patel
  • 107
  • 9