Jump to content
  • Advertisement
Sign in to follow this  
Ludi83

Static in C?

This topic is 5009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!