The static has several uses in C. For example, in the C source file at the top, you can:
The fileVisibleInt variable is created and initialized while the application is loading, but if you try to access it from some other compilation unit, you will receive an error message from the linker when you try to link.
You can also use static in a function to create a variable that will exist and maintain state.
int myFunc (int k) { static int mySavedInt = 0; // create and initialize a permanent int variable if (k > 10) { mySavedInt = k; // use the variable to save a value for the next time function is called } else if (mySavedInt > 22) { // do some stuff } }
The point at which the static variable is visible to another source in the file is at the point where it appears in the source file, and its visibility is determined by various scope rules. For example, static defined in a function is visible only in this function or if static used in some other, more limited area, such as if or for , then it is a constant variable that is only visible in this area.
There are various phrases that are general, but not necessarily technically accurate, or something you will find in the standards. For example, the phrase "global reach" means that the thing is visible outside the compilation unit. Under the "module" I would take a function. For most everyday activities, language weakness works great.
In C ++, static may be slightly different depending on whether you use C ++ constructs such as class and using static as a qualifier for methods and members, or if you use its old C method.
See also:
File scope and global scope: C and C ++ .
Why should file scope static variables be initialized to zeros?
Dr. Dobbs: Scopes in C ++ .