11

As per my understanding, each called function has some memory allocated to it in the program stack, and this holds true even if the same function calls itself recursively (i.e, each invocation has it's own memory in program stack). Please answer the following two questions arising from my program below:

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

If the variable is not declared static (e.g, simply "int x"), will each recursive call to the function have its own copy of that variable? If yes, is that the way it normally happens when a function is called from other function, including the recursive calls?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    static int x=0;
    x++;
    printf("Team %d\n",x);
    if(x<10)
        main();
    else
        exit;
}

Output:

Team 1
Team 2
Team 3
Team 4
Team 5
Team 6
Team 7
Team 8
Team 9
Team 10
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Meathead
  • 493
  • 2
  • 6
  • 15
  • 8
    You already have your answer in the output. Do you have any doubts still? – R Sahu Jun 18 '14 at 02:50
  • What @RSahu said, why not continue to test it out for yourself. – bwegs Jun 18 '14 at 02:51
  • 1
    @dasblinkenlight Only in C++ in C it is not undefined to call main, although I would not recommend it. – Shafik Yaghmour Jun 18 '14 at 02:52
  • But you should absolutely avoid doing it anyway. – Fantastic Mr Fox Jun 18 '14 at 02:53
  • @RSahu Why else do you think I posted the question?Isn't it obvious that I still had doubts? The three people who were nice enough to answer may not have as much reputation as you have, but were way more helpful.And yeah, thanks for the downvote. – Meathead Jun 22 '14 at 12:02
  • @Newbie I voted to close the question since it's a duplicate but I did not downvote. Please don't make assumptions that are not true and hold a grudge based on those assumptions. I hope we meet again in a different question with a more positive interaction. – R Sahu Jun 22 '14 at 14:29

3 Answers3

18

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

Yes, as your code sample shows

If the variable is not declared static (e.g, simply "int x"), will each recursive call to the function have its own copy of that variable? If yes, is that the way it normally happens when a function is called from other function, including the recursive calls?

Yes, each call will have it's own stack frame with a copy of each local variable. This is what happens for every function call, whether they are recursive or not. And yes, this very principle allows, among other things, recursion.

quantdev
  • 23,517
  • 5
  • 55
  • 88
8

Static variables live the life of the program, there is only one copy

Locals (Autos) get a new copy on the stack each function call.

Parameters passed by value get a new copy as well.

Parameters passed by reference (pointer) point to the copy that's passed to them. Therefore if it's a local in a recursive execution, it might be a unique copy; but usually it's a reference to a variable that called the function before the recursion started, therefore, the same copy.

It's better to do the functionality you're doing by passing a pointer to a variable, you have more control over subsequent instances of recursion.

Al Bud
  • 81
  • 1
  • 1
4

Your understanding fairly accurate.

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. A global variable can also be marked static which means its scope is limited to that file.

When a function is called its local variables are placed on the call stack. This stack is pushed for each function call and then popped when each function exists. A recursive call just keeps pushing a new copy of the same function onto the stack.

Sean Perry
  • 3,776
  • 1
  • 19
  • 31