It seems to me like there are several conflicting explanations on the use of static variables.
There are two main types of static variables:
- Static variables declared inside functions
- And static variables declared outside functions
The first type means that the variable is persistent between calls of the function.
Let’s say I have the function below:
void function (void) {
static int counter = 0;
int fakeCounter;
counter++;
printf("Counter: %d
", counter);
printf("Fake Counter: %d
", fakeCounter);
}
Here, the variable counter is initialized to 0 at compile time. When the function is first called it will print out “Counter: 1”, the second time it will print out “Counter: 2”, and so on. As you can see, the static variable keeps its value between calls. In this case, the keyword static refers to the static storage class. In the static storage class, the data is stored in normal RAM and the variable in the static storage class will always be guaranteed to stay the same unless you explicitly change their value. Therefore, it is unlike a normal variable inside a function, which is in the “auto” storage class. Variables in the auto storage class are stored in the “stack”, which is sort of like a piece of scratch paper used by the function on which to store information. Since this scratch paper gets “thrown away” when the function is completed, variables in the auto class are not persistent and can be used for internal work within a function. (One disadvantage to using this piece of scratch paper is that more instructions are necessary to access the variable) Therefore, you can NEVER replace a normal (i.e. auto) variable with a static variable.
----You can skip reading this if you wish, but it is interesting:) ----
However, I believe you can use the “overlay” storage class in order to save CPU time since the variable is stored in normal RAM rather than the function stack. However, the variable is reinitialized upon every call of the function and other functions with overlay variables are allowed to use the memory locations of other overlay variables that are in functions which do not call the current function. See example below:
void f1(void) {
overlay int i;
i = 0;
f2();
}
void f2(void) {
overlay int j;
j= 2;
}
void f3(void) {
overlay int k;
k = 3;
}
In the above set of functions “k” can be stored in the same location as i or j, but i and j obviously cannot share the same location. Therefore, I believe you can use overlay variables in place of normal auto variables wherever you wish (unless you are using recursion). However, you may end using more memory though if you have a lot of nested functions.
----Start reading again----
Now after this long digression, the explanation of what the other “static” means.
When static is used with a global variable, it means that the global variable cannot be accessed from outside the file in which it is declared. That is, if i declare static int a = 0 in file1.c a function in file2.c cannot access the variable “a”. This version of the static modifier can also be used with functions in order to indicate that the function cannot be called from outside the file.
This isn’t true, static variables are not an optimization. They in fact make your code more readable. (However, if you replace static with overlay this is true.) However, I definitely do agree with the rest of EricVanWyk’s post.
I hope this insanely long and boring post was helpful:p