In C programming, a static variable is a variable declared
using the static keyword. One of its most important
characteristics is that its stored value is preserved between
function calls.
Unlike an ordinary local variable, which is created when a function is entered and normally loses its value when the function returns, a local static variable is initialized only once and continues to exist for the entire execution of the program.
#include
void counter()
{
static int count = 0;
count++;
printf("Count = %d\n", count);
}
int main()
{
counter();
counter();
counter();
return 0;
}
The output will be:
Count = 1
Count = 2
Count = 3
Here, count is a static local variable. Although the function
counter() is called three times, count is
initialized to 0 only once. Its value is preserved after each
function call.
Consider the following two functions:
void normalCounter()
{
int count = 0;
count++;
printf("%d\n", count);
}
void staticCounter()
{
static int count = 0;
count++;
printf("%d\n", count);
}
If each function is called three times, the output will be different:
normalCounter();
normalCounter();
normalCounter();
staticCounter();
staticCounter();
staticCounter();
The ordinary local variable is initialized each time the function is called, so its output will be:
1
1
1
The static local variable retains its previous value, so its output will be:
1
2
3
static keyword.
The static keyword has another important use in C. When
static is applied to a variable declared at file scope, it
gives that variable internal linkage. This means the
variable can be accessed only within the same source file.
static int total = 100;
In this case, total is not a local variable. It is a
file-scope static variable and cannot be accessed directly from another
source file using an external declaration.
It is important to distinguish between scope and lifetime. For a local static variable, the scope may be limited to the function or block where it is declared, but its lifetime lasts for the entire execution of the program.
| Property | Static Local Variable | Ordinary Local Variable |
|---|---|---|
| Keyword | static |
No static keyword |
| Initialization | Performed only once | Normally performed each time execution reaches the declaration |
| Value between function calls | Retained | Not retained after the function returns |
| Lifetime | Entire program execution | Typically limited to each execution of its block |
| Scope | Block or function where declared | Block or function where declared |
A static local variable can be thought of as a variable that remembers its previous value even after the function that contains it has finished executing.
For example:
static int count = 0;
The first time the function runs, count starts at
0. If the function increases it to 1, the next
time the function is called, count starts from
1 rather than being reset to 0.
Therefore, the key idea behind a static local variable is: it has a limited scope but retains its stored value throughout the lifetime of the program.