3

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()?

9
  • 2
    Possible duplicate of What does "static" mean in a C program? Commented Apr 6, 2016 at 9:43
  • @ameyCU That's not what this question is about. Commented Apr 6, 2016 at 9:45
  • @Lundin I think answer for that question answer these . Commented Apr 6, 2016 at 9:48
  • Iam just interested when I should use method(1) and when (2) and why ;)? Commented Apr 6, 2016 at 9:49
  • If you declare it in function it will available in function scope, otherwise it will available in file scope... sometimes you don't need or want it in file scope... Commented Apr 6, 2016 at 9:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.