What I want to do is have an auxiliary function inside my .c file that will only be used within another function in the file, and has no use outside that specific case. Therefore, it's not of great use for the interface to contain it, but I'm just wondering whether that is possible and/or a good practice, as well as if there's an alternative that is more elegant.
Asked
Active
Viewed 89 times
1 Answers
1
By not declaring the function in the header file, you are simply ommiting information about the function, not the visibility of the function.
Declare it static
(and omit from the header file).
// barfoo.h
int bar(void);
//barfoo.c
#include "barfoo.h"
static int foo(void) { return 42; }
int bar(void) { return foo(); }
//main.c
#include "barfoo.h"
int main(void) {
bar();
}
In this example, if foo()
wasn't defined with static
it could still be called from main()
. With static
the linker will report an error if you try to use it.

pmg
- 106,608
- 13
- 126
- 198