Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
static is an unfortunately heavily overloaded keyword. At "file scope" Symbols are said to be at "file scope" if their definition is not inside a function. When the static keyword appears at fil...
Answer
#1: Initial revision
`static` is an unfortunately heavily overloaded keyword. ## At "file scope" Symbols are said to be at "file scope" if their definition is not inside a function. When the `static` keyword appears at file scope, it specifies the **linkage** of the symbol to which it is applied. "Linkage" in this case means "visibility outside the current translation unit." So a variable name or a function name that are *not* declared `static` will default to being visible to outside translation units that may want to link to them ("reference" them, in linker terms). But a symbol marked `static` is *not* visible outside the current translation unit. So at file scope, `static` means `private`. ## At "function scope" When the `static` keyword appears at function scope -- that is, when it applies to a symbol defined inside a function -- it specifies the **storage duration** of the symbol to which it is applied. You will know that with the exception of GCC, which provides an extension for the purpose, functions cannot be defined inside other functions. So `static` at function scope will be applied only to variables. By default, variables at function scope have *automatic* storage duration. This means they are created on the stack, and cease to exist when control leaves the scope of the function. (They are said to be "on the stack" because most CPUs support quickly allocating storage on the stack for this purpose.) But variables inside a function that are declared `static` become persistent. Their storage duration is "static," meaning that the value is not stored on the stack. It is stored in the global data area, defaults to 0, is initialized before `main` is called, etc. All the things that are true of global (file scope) variables are also true of `static` variables at function scope. Most importantly, `static` variables in a function hold their value from one call to the next. ## At "parameter scope" The use of the `static` keyword for parameters is just "we need a keyword, and don't want to introduce a new one." It basically says "the pointer being passed is not null, and points to at least this much storage." It has no relation to the other meanings of `static`.