I'm writing a library in c and there are some functions I would like to be callable from other c file and some I would like to keep private.
I know that it is possible to hide functions outside of the library file by declaring it static
but the same can be achieved by declaring only the implementation on it and leaving it out from the header.
Compare:
a.h
//static int private_routine();
int sample();
a.c
/*static*/ int private_routine(){
}
int sample(){
private_routine();
}
Which practice is best?
To keep functions "private", or better, in scope of a single file, you need to declare them static
. Otherwise you can redeclare the same function in other file and still use it.
For example:
#include "a.h" // without declaration of private_routine()
/*extern*/ int private_routine(); // by default every declaration behaves as with extern keyword, which means function is defined elsewhere
int main()
{
private_routine(); // can use here
return 0;
}
EDIT: And as pointed in comments, it's not possible to define function with same name multiple times in one application without declaring it static
. If you define a given function name N times, at least N-1 of those must be static
.