Possible Duplicates:
What does “static” mean in a C program?
Static vs global
What does "static" mean in C, giving the following example: "static struct ........"?
And what is the diffrence between this and "struct ......" without the "static"?
Possible Duplicates:
What does “static” mean in a C program?
Static vs global
What does "static" mean in C, giving the following example: "static struct ........"?
And what is the diffrence between this and "struct ......" without the "static"?
Outside a function, static makes whatever it's applied to have file scope. For example:
int a_function(int x) { ... }
This function will have global linkage, and can be accessed by any other object file. You just have to declare it to use it, as is usually done in a header file:
int a_function(int x);
However, if you use static in the definition, then the function is visible only to the source file where it is defined:
static int a_function(int x) { ... }
In that case, other object files can't access this function. The same applies to variables:
static int x;
This makes x a global variable, visible only within it's source file. A "static struct" by itself doesn't do anything, but consider this syntax:
struct {
int x;
int y;
} p1, p2;
This declares two global variables (p1 and p2), each of an "anonymous" struct type. If you append static:
static struct {
int x;
int y;
} p1, p2;
Then static applies to p1 and p2, making them visible only within their source file.
static tells that a function or data element is only known within the scope of the current compile.
In addition, if you use the static keyword with a variable that is local to a function, it allows the last value of the variable to be preserved between successive calls to that function.
So if you say:
static struct ...
in a source file no other source files could use the struct type. Not even with an extern declaration. But if you say:
struct ...
then other source files could access it via an extern declaration.
I'm not a C programmer, but if static
in C means anything like it does in other languages I use STATIC STRUC, meaning that the structure is common amongst all instances of this class.
Say I had a class variable called Z. The usual behaviour is that the value of this variable is specific to a particular instance of a classs, but when it is static, all instances of the class share the same value of Z at all times.
I don't know how this applies to C, isn't C object-less?