2

Lets say I have a function void func() and I use the variable testVar inside of this function. Additionally I need that the variable persists after leaving the function. Usually I would do it by using a static variable inside of the function(1). But what are the differences, when I would use a global variable instead(2)?

static int testVar = 0; //global variable instead(2)

void func()
{
    static int testVar = 0; //static variable inside of the function(1)

    if(testVar++){
        //do something
    }   
}

What does the compiler do in both cases in detail? Is there a scenario where I should use method(2) provided the variable is only needed in func()?

Dev Null
  • 4,731
  • 1
  • 30
  • 46
xy36
  • 315
  • 1
  • 11

2 Answers2

5

The only difference is that when the variable is declared inside the function, it has local scope only to that function. To reduce the scope of a variable as much as possible is always good practice, so if this is possible, you should do so.

Though sometimes when implementing ADTs and similar modules, you want a local variable to be accessible by several functions. In that case you declare it at file scope.

In some cases, placing variables at file scope is bad programming practice, since it makes the code harder to read and maintain. It is the same problem as why global variables are considered bad, even though a static file scope variable is not global (it is not visible by the whole program).

In other cases, most notably embedded systems, where there is only one instance of the "class" you are writing (singleton), then using file scope static variables is common practice, as a poor man's way to do private encapsulation.

Lundin
  • 195,001
  • 40
  • 254
  • 396
3

here is some adjusted code to show the differences between where the variables are defined: (from : What does "static" mean?)

#include <stdio.h>

//static int sa = 10;

void func1()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf("func 1: a = %d, sa = %d\n", a, sa);
}

void func2()
{
    int a = 10;
    static int sa = 10;

    a += 5;
    sa += 5;

    printf(" func2 : a = %d, sa = %d\n", a, sa);
}


int main()
{
    int i,j;

    for (i = 0; i < 10; ++i){
        func1();
    }

    for (j = 0; j < 10; j++)
    {
        func2();
    }
}

produces:

func 1: a = 15, sa = 15
func 1: a = 15, sa = 20
func 1: a = 15, sa = 25
func 1: a = 15, sa = 30
func 1: a = 15, sa = 35
func 1: a = 15, sa = 40
func 1: a = 15, sa = 45
func 1: a = 15, sa = 50
func 1: a = 15, sa = 55
func 1: a = 15, sa = 60
 func2 : a = 15, sa = 15
 func2 : a = 15, sa = 20
 func2 : a = 15, sa = 25
 func2 : a = 15, sa = 30
 func2 : a = 15, sa = 35
 func2 : a = 15, sa = 40
 func2 : a = 15, sa = 45
 func2 : a = 15, sa = 50
 func2 : a = 15, sa = 55
 func2 : a = 15, sa = 60

while

#include <stdio.h>

static int sa = 10;

void func1()
{
    int a = 10;
//  static int sa = 10;

    a += 5;
    sa += 5;

    printf("func 1: a = %d, sa = %d\n", a, sa);
}

void func2()
{
    int a = 10;
    //static int sa = 10;

    a += 5;
    sa += 5;

    printf(" func2 : a = %d, sa = %d\n", a, sa);
}

produces

func 1: a = 15, sa = 15
func 1: a = 15, sa = 20
func 1: a = 15, sa = 25
func 1: a = 15, sa = 30
func 1: a = 15, sa = 35
func 1: a = 15, sa = 40
func 1: a = 15, sa = 45
func 1: a = 15, sa = 50
func 1: a = 15, sa = 55
func 1: a = 15, sa = 60
 func2 : a = 15, sa = 65
 func2 : a = 15, sa = 70
 func2 : a = 15, sa = 75
 func2 : a = 15, sa = 80
 func2 : a = 15, sa = 85
 func2 : a = 15, sa = 90
 func2 : a = 15, sa = 95
 func2 : a = 15, sa = 100
 func2 : a = 15, sa = 105
 func2 : a = 15, sa = 110

summary comments (from the original reference): What does "static" mean?

  • Static defined local variables do not lose their value between function calls. In other words they are global variables, but scoped to the local function they are defined in.

  • Static global variables are not visible outside of the C file they
    are defined in.

  • Static functions are not visible outside of the C file they are
    defined in.

Community
  • 1
  • 1
Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
  • 1
    The proper definition of a global variable is a variable which is globally visible to the whole program. Thus a `static` variable can never be global. Don't mix up "global" with "file scope", they are different terms. – Lundin Apr 06 '16 at 09:53
  • Thanks for the update, but this is not what I am asking for. The question was for a scenario, when i use the var in one function. – xy36 Apr 06 '16 at 10:47