#include <stdio.h>
void increment() {
static int count = 0;
count++;
printf("Count is %d\n", count);
}
void use_increment() {
increment();
}
int main() {
increment();
use_increment();
use_increment();
increment();
return 0;
}
/*
The static keyword is used to declare variables that are allocated memory in data
segment and it exists for the entire lifetime of the program. They are initialized
only once at the beginning of the program execution and retain their value until
the program ends.
There are mainly two use cases for the static keyword:
To limit the scope of the variable to the file where it is declared. When a variable
is declared static inside a function, it retains its value even after the function
returns. It can be used for implementing persistent local variables in a function.
To implement global variables that can be accessed by multiple functions within the
same file. A static variable declared outside of any function is a global variable
that is only visible within the file where it is declared.
Overall, static variables provide a way to store data that needs to be persistent
across function calls or needs to be shared among multiple functions within the
same file.
*/
To embed this program on your website, copy the following code and paste it into your website's HTML: