I\'ve seen the word static
used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
Static variables in C have the lifetime of the program.
If defined in a function, they have local scope, i.e. they can be accessed only inside those functions. The value of static variables is preserved between function calls.
For example:
void function()
{
static int var = 1;
var++;
printf("%d", var);
}
int main()
{
function(); // Call 1
function(); // Call 2
}
In the above program, var
is stored in the data segment. Its lifetime is the whole C program.
After function call 1, var
becomes 2. After function call 2, var
becomes 3.
The value of var
is not destroyed between functions calls.
If var
had between non static and local variable, it would be stored in the stack segment in the C program. Since the stack frame of the function is destroyed after the function returns, the value of var
is also destroyed.
Initialized static variables are stored in the data segment of the C program whereas uninitialized ones are stored in the BSS segment.
Another information about static: If a variable is global and static, it has the life time of the C program, but it has file scope. It is visible only in that file.
To try this:
static int x;
int main()
{
printf("Accessing in same file%d", x):
}
extern int x;
func()
{
printf("accessing in different file %d",x); // Not allowed, x has the file scope of file1.c
}
run gcc -c file1.c
gcc -c file2.c
Now try to link them using:
gcc -o output file1.o file2.o
It would give a linker error as x has the file scope of file1.c and the linker would not be able to resolve the reference to variable x used in file2.c.
References:
It depends:
int foo()
{
static int x;
return ++x;
}
The function would return 1, 2, 3, etc. --- the variable is not on the stack.
static int foo()
{
}
It means that this function has scope only in this file. So a.c and b.c can have different foo()
s, and foo is not exposed to shared objects. So if you defined foo in a.c you couldn't access it from b.c
or from any other places.
In most C libraries all "private" functions are static and most "public" are not.
It is important to note that static variables in functions get initialized at the first entry into that function and persist even after their call has been finished; in case of recursive functions the static variable gets initialized only once and persists as well over all recursive calls and even after the call of the function has been finished.
If the variable has been created outside a function, it means that the programmer is only able to use the variable in the source-file the variable has been declared.
People keep saying that 'static' in C has two meanings. I offer an alternate way of viewing it that gives it a single meaning:
The reason it seems to have two meanings is that, in C, every item to which 'static' may be applied already has one of these two properties, so it seems as if that particular usage only involves the other.
For example, consider variables. Variables declared outside of functions already have persistence (in the data segment), so applying 'static' can only make them not visible outside the current scope (compilation unit). Contrariwise, variables declared inside of functions already have non-visibility outside the current scope (function), so applying 'static' can only make them persistent.
Applying 'static' to functions is just like applying it to global variables - code is necessarily persistent (at least within the language), so only visibility can be altered.
NOTE: These comments only apply to C. In C++, applying 'static' to class methods is truly giving the keyword a different meaning. Similarly for the C99 array-argument extension.
Short answer ... it depends.
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.
A static variable value persists between different function calls andits scope is limited to the local block a static var always initializes with value 0