Variables that survive within a function
The lifetime of a static local variable.
We have discussed local variables, global variables, and variables with different lifetimes.
Actually, there exists a variable with unusual characteristics that lies between these two.
When declaring a variable within a function, adding static before the type name
You can declare a static local variable.
The following program demonstrates an example of declaring a static local variable.
The results of this program are as follows:
Despite being declared within the function, its value increases by one with each call.
The initial value is 0 even though it hasn't been initialized.
It looks like a global variable, plain and simple.
However, the variable count is declared within the function, making it essentially a local variable.
In fact, using the variable `count` within the `main` function will result in an error.
This is a characteristic of a static local variable.
Since it's declared within a function, it's only accessible within that function.
That value persists until the program terminates.
Furthermore, they are automatically initialized to zero, especially without explicit initialization.
Moreover, initialization will only be performed once at the beginning.
For example, it can also be counted if initialized like this.
This variable is used when you want the function to remember a value from a previous call.
While its uses are limited, it can be helpful for counting function calls.
This could be the case when a search function needs to remember previously found character positions.
Actually, there exists a variable with unusual characteristics that lies between these two.
When declaring a variable within a function, adding static before the type name
You can declare a static local variable.
The following program demonstrates an example of declaring a static local variable.
Source code
#include <stdio.h>
int countfunc(void);
int main(void)
{
countfunc();
countfunc();
countfunc();
return 0;
}
int countfunc(void)
{
static int count; /* Static Local Variable */
count++;
printf("%d\n", count);
return count;
}
The results of this program are as follows:
Results
1
2
3
2
3
Despite being declared within the function, its value increases by one with each call.
The initial value is 0 even though it hasn't been initialized.
It looks like a global variable, plain and simple.
However, the variable count is declared within the function, making it essentially a local variable.
In fact, using the variable `count` within the `main` function will result in an error.
This is a characteristic of a static local variable.
Since it's declared within a function, it's only accessible within that function.
That value persists until the program terminates.
Furthermore, they are automatically initialized to zero, especially without explicit initialization.
Moreover, initialization will only be performed once at the beginning.
For example, it can also be counted if initialized like this.
Source code
static int count = 0; /* Static Local Variable */
This variable is used when you want the function to remember a value from a previous call.
While its uses are limited, it can be helpful for counting function calls.
This could be the case when a search function needs to remember previously found character positions.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




