Reputation: 139
[Ansi-C/Eclipse] For some reason I did not find anything helpful so here is my Question:
I want bar()
to be only accessible to code inside foobar.c
Should I:
put static in front of the declaration and the definition
remove the declaration of bar()
from foobar.h
and put it in foobar.c
?
What is the difference? This is my setup:
main.c:
#include "foobar.h"
int main() {
foo();
}
foobar.h:
#ifndef FOOBAR
#define FOOBAR
void foo();
void bar(); //Move to foobar.c?
#endif
foobar.c:
#include "foobar.h"
void foo() {
bar();
}
void bar() { //make this static?
printf("Hello World");
}
Upvotes: 0
Views: 69
Reputation: 134336
Yes, you're absolutely right. You should do both.
making a function static
limits the visibility of that function to the translation unit (file) only. That function cannot be called from any other function present in any other source file.
You don't need to call a static
function from some other translation unit, hence you don't need a prototype in header file. If required, you can put a forward declaration inside foobar.c
itself.
Upvotes: 1