Search code examples
cstatickeywordansi-c

What does static mean in ANSI-C


Possible Duplicate:
What does “static” mean in a C program?

What does the static keyword mean in C ?

I'm using ANSI-C. I've seen in several code examples, they use the static keyword in front of variables and in front of functions. What is the purpose in case of using with a variable? And what is the purpose in case of using with a function?


Solution

  • Just as a brief answer, there are two usages for the static keyword when defining variables:

    1- Variables defined in the file scope with static keyword, i.e. defined outside functions will be visible only within that file. Any attempt to access them from other files will result in unresolved symbol at link time.

    2- Variables defined as static inside a block within a function will persist or "survive" across different invocations of the same code block. If they are defined initialized, then they are initialized only once. static variables are usually guaranteed to be initialized to 0 by default.