Example
Code:
void foo(void)
{
static int var = 7;/* This runs only once*/
printf("%d\n", var);
var++;
}
int main(void)
{
// call foo 4 times
foo();
foo();
foo();
foo();
return 0;
}
The output would be:
7
8
9
10
Why?
As you may know, local variables terminate their lifetime, when the function they live in terminates.
var, in our example, is a static variable though. As a result, it is "immortal". It won't die, no matter how many times the function the terminates. It is created once and dies once, when your program ends.
As a result, the value of the variable is remembered among function calls!
But, this line of code
Code:
static int var = 7;
runs only once, because the static variable is created one time only!
Notice, that static keyword can also been found for limiting the scope of a variable, making her have in -file scope. However, the static keyword has not the same effects on these situations.
//Also, Sam, welcome to the forum