In C - There are essentially three uses of the word 'static'. It is important to note here that the usage is slightly different from C++, due to the non-existence of classes.
The threes instances of use are:
1) A variable declared as static inside a function.
2) A variable declared as static at the file scope level, accessible to all functions in that file.
3) A function defined as 'static' within a .c file.
Explanation or usage:
First Case:
The first one is most commonly used in projects and is fairly well understood. It relates to a variable retaining its value between function invocations.
i.e if you have:
void foo()
{
static uint8_t testingStaticVariable = 0;
testingStaticVariable += 1;
printf("Value of Static variable is %d", testingStaticVariable);
}
void main()
{
for(int i = 0; i < 3; i++)
{
foo(); // Called three times.
}
}
For the above mentioned sample program, you will find that the testingStaticVariable will retain its value everytime foo() is called.
Therefore the output would look like this:
Value of Static variable is 1
Value of Static variable is 2
Value of Static variable is 3
Therefore you can see that by defining the variable as static it retained it's value between the multiple invocations of foo.
Second case:
Defining a variable as 'static' in a example.c file simply means that all functions within that example.c file have access to alter the contents of this variable. However, any functions that might include the header file example.h for this example.c file, will not have access to this variable.
This is also sometimes known as localised global.
Third case:
This is probably the least used, but is extremely useful, if one knows how to utilize this feature. There are no access specifiers for the C programming language. i.e there are no functions you can declare private or public.
However, you can almost achieve the same level of protection, by declaring functions as static. If you declare a function as static within a .c file, only other functions within that .c file can see/access this function, whereas the interface or the header file .h will not contain these function prototypes, and therefore, any files within the project that might include the header file, will still not have access to the 'private' (by declaring them as static) functions defined in the .c file.
This achieves some level of protection or abstraction, that is really useful in large projects, where you want to be able to provide an interface (via the header file) to this .c file or more appropriately referred to as a compilation unit, but do not want to expose other functions defined within this compilation unit.
So essentially you are localising the scope of these functions to a particular compilation unit, such that even though other modules might have access to this module, they will be unable to see these functions.
Typically the way to achieve this is:
You would have:
an interface file (visible to everyone in the project) called example.h
a source file that contains the implementation of the interface, called example.c
//example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
void doSomethingImp();
#endif
//example.c
// global variable definition
uint8_t exampleVariable = 0;
// local function prototypes declared here as static, not visible outside example.c
static void setVariable(uInt8_t num);
static uint8_ t geVariable();
void doSomethingImp()
{
//lets do something here with a variable.
uint8_t foo = 0;
setVariable(5);
foo = getVariable();
}
static void setVariable(uInt8_t num)
{
exampleVariable = num;
}
static uint8_ t geVariable()
{
return exampleVariable;
}
Now in the above mentioned sample project, if another foo.c included example.h, it would only have access to the doSomethingImp() function. It WOULD NOT HAVE access to the setVariable(uInt8_t num) and the geVariable() functions since they were declared as static, which localised their scope to the example.c compilation unit.