Lesson 8.7. Static variables

Local variable

A local variable is necessarily declared inside a function. The memory is allocated at the time of its declaration and is freed as soon as the execution of the function ends. Therefore, its content is lost as soon as the function is exited.

Let's analyze the following example:

int main(void) {
  fct ();
  fct ();
  return 0;
}

void fct(void) {
  int i=0;
  printf ("%d\n",i++);
}

The above program uses a function fct() which contains a local variable i which is initialized to 0 each time the function is called. We deduce that the function will always display 0.

Static variable

A static variable is also a variable declared inside a function. The difference between a local variable and a static variable is that the memory of a static variable is not freed at the end of the function. The value of a static variable is even kept from one call of the function to the next. A static variable is declared with the keyword static placed before the type of the variable:

static int i;

Let's take the previous example and transform the local variable i into a static variable:

void fct(void) {
  static int i=0;
  printf ("%d\n",i++);
}

This time we can see that the variable i is kept from one call to another of the the function. This mechanism can be useful, for example, to count the number of times a function is called.

Exercise

We provide the code of the function factorial() which returns the factorial of an integer passed in parameter.

unsigned long int factorial(unsigned int n) {
  if (n==0) return 1;
  return n*factorial(n-1);
}

Recall that the factorial is defined by :

$$ !n = 1 \times 2 \times 3 ... \times (n-1) \times n $$

Note that this function is recursive, i.e. that it calls itself. The calculation is done thanks to the following formula:

$$ !n = n \times !(n-1) $$

Use a static variable to count the number of times the function is called in the calculation of \(!7\)).

Quiz

Static variables are used to

Check Bravo! A static variable is used to keep the value of the variable from one call to the next. Try again...

The memory of static variables ...

Check Bravo! The memory is allocated at declaration time, but it is not freed at the end of the function. Try again...

What does the following program display?

int main(void) {
  fct();
  fct();
  fct();
  fct();
  fct();
  return 0;
}
void fct(void) {
  int i=0;
  printf ("%d", i++);
}
Check Bravo! The local variable i of the function is initialized to 0 at each call. Try again...

What does the following program display?

int main(void) {
  fct();
  fct();
  fct();
  fct();
  fct();
  return 0;
}
void fct(void) {
  static int i=0;
  printf ("%d", i++);
}
Check Bravo! i is a static variable, so its value is kept from one call to another of the fct() function. Try again...

What does the following program display?

int main(void) {
  int i;
  for (i=10;i<15;i++)
    fct();
  return 0;
}
void fct(void) {
  static int i=0;
  printf ("%d", i++);
}
Check Bravo! The variables i of main() and of the function are independent. Try again...

See also


Last update : 11/21/2022