I know that static function's name is visible only within the file (translation unit) in which it's declared. This makes encapsulation possible.
But static function is usually declared in the source file because if you do it in the header file, you can end up with multiple implementations of it (which I think is not the intention of static
).
Example:
main.c
#include "functions.h"
int main()
{
FunctionA();
FunctionB(); // Can't call regardless of "static".
return 0;
}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void FunctionA();
#endif /* FUNCTIONS_H */
functions.c
#include "functions.h"
#include <stdio.h>
static void FunctionB(); // Same whether "static" is used or not.
void FunctionA()
{
printf("A");
}
void FunctionB()
{
printf("B");
}
So when is static
useful?