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.
Comments on What does the static keyword do in C?
Parent
What does the static keyword do in C?
What exactly does the static
keyword do in C? I have seen it used in several diverse contexts:
1) As a variable outside a function:
#include <stdio.h>
static int x = 5;
int main (void)
{
printf("%d\n", x);
}
2) As a variable inside a function:
void func (void)
{
static int count=0;
printf("%d\n", count++);
}
3) As part of a function declaration/definition:
static void func (void);
4) As part of a function parameter list containing an array:
void func (int arr[static 5]);
All of these uses seem quite unrelated. What exactly is the purpose of static
in all the above examples?
Post
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
.
1 comment thread