fallingcats
fallingcats

Reputation: 139

C Functions: static or remove from header

[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:

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

Answers (1)

Sourav Ghosh
Sourav Ghosh

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

Related Questions