Skip to main content
gamedev.net
🔒 Locked

Static in C?

Started by Ludi83 0 replies 850+ views
Original Post
Ludi83
Ludi83
Hi, what is the sense of static in C? I don't get it. I've read the MSDN and another C-Book, but my brain won't understand this. the msdn says
Quote:
When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).
static duration: and what about global vars? they don't have static duration? huh? variable not visible from outside: i can't see anythink from the outside in C without using extern, or am i wrong? So, if anybody could help to disentangle my brain, then please, do it :)
Fruny
Fruny
Static variable (or function) at file scope: the variable has internal linkage, i.e. it is local to this translation unit. Different .c files can thus have their own copy (or version) of the variable (or function). Without it, you will get linker errors (multiple definition).

Static variable at function scope: the variable really is a kind of global variable, which gets intialized the first time the function is called (and only then), and keeps its value across multiple calls to that function.

Quote:
static duration: and what about global vars? they don't have static duration? huh?


Yes, they do. But variables in functions have auto duration - unless you make them static.

Quote:
variable not visible from outside: i can't see anythink from the outside in C without using extern, or am i wrong?


extern marks a declaration, allowing you to use the variable, so long as it has been defined in one of the translation units you eventially link together. Even extern will not let you access a variable with internal linkage that is defined in another translation unit.

Note - use
for quotes.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.