Documente Academic
Documente Profesional
Documente Cultură
As a sidenote, both global and static variables have static initialization, which means that if you
don't set them to a value, they will be initialized to 0 (common variables) or NULL (pointers).
This is the only case in ANSI C where you can assume that the value is zero without initializing
it explicitly.
Static global variables: variables declared as static at the top level of a source file
(outside any function definitions) are only visible throughout that file ("file scope", also
known as "internal linkage").
Static local variables: variables declared as static inside a function are statically
allocated, thus keep their memory cell thoughout all program execution, while having the
same scope of visibility as automatic local variables, meaning remain local to the
function. Hence whatever values the function puts into its static local variables during
one call will still be present when the function is called again.
The meaning of the "static" keyword in C depends on whether or not it appears within a bracedelimited block. Refer to the following code snippet.
1.
2.
int a;
static int b;
3.
4.
int func(void) {
5.
int c;
6.
static int d;
7.
...
8.
Variables 'a' and 'b' are declared outside of any brace-delimited blocks, therefore they both
persist for the life of the program and are accessible from anywhere in the compilation unit.
In this case, the "static" keyword has the effect of making variable 'b' local to the compilation
unit. Variable 'a' can be accessed from any other compilation units that contain an extern
declaration for it.
Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only
accessible from within that block. In this case, the "static" keyword has the effect of making
variable 'd' persist for the life of the program. Variable 'c' only persists while execution is
within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the
same location for successive executions of the brace-delimited block.
Performance Impact
http://stackoverflow.com/questions/3835857/in-c-does-using-static-variables-in-afunction-make-it-faster