Reputation:
I have a static function in a source file that is used by other functions in that same source file. Is it fine to put the declaration for that static function in the header file, even though that header file will be included into other source files? Example:
/* foo.c */
#include "foo.h"
/* exported function; calls g */
void f(void) {
g();
}
static
void g(void) {
/* do something... */
}
/* foo.h */
void f(void);
void g(void);
/* main.c */
#include "foo.h"
int main(void) {
f();
}
Upvotes: 0
Views: 1107
Reputation: 153456
Is it fine to put the declaration for that static function in the header file, even though that header file will be included into other source files?
No. Consider the conflict the other source files would have if they had a function/object/macro of the same name.
Even without conflict, an "unused function" warning may occur. @Adrian Mole
By putting static void g(void)
in the .h file, it adds an unnecessary potential name conflict.
Simply declare/define that static function at the top of the .c in which it is used.
Upvotes: 1
Reputation: 2930
Is it fine to put the declaration for that static function in the header file, even though that header file will be included into other source files?
No. becuse it does not make any sense. and why?
In C
, functions are global
by default. Unlike global functions
in C, access to static functions
is restricted to the file where they are declared! Therefore, when we want to restrict access
to functions, we make them static.
Another reason for making functions static can be reuse of the same function name in other files. so declaring them static in the header file can cause to name collisons in other source files.
In header files we declare our API functions those we want to expose only. in c files we declare usually auxilary functions as static functions for restricting the scope of those aux functions to the c file only
Upvotes: 0