Reputation: 325
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()
?
Upvotes: 3
Views: 2364
Reputation: 5075
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.
Upvotes: 3
Reputation: 215305
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.
Upvotes: 5