4

When I write software in C, should I put static and inline functions in the .h or in the .c file?

syntagma
  • 23,346
  • 16
  • 78
  • 134

2 Answers2

3

Do not put the declaration nor definition of a static function (or variable) in a .h file. That defeats the points of static - keeping functions/variables local.

inline is another matter - it depends on the scope meant for the function. An inline function in a .h is meant for global usage and scope. An inline function in a .c is meant for local usage only. This is much the same strategy as would be used for a #define

inline functions can be extern, static or unspecified. @Christoph well explains inline scope issues.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • True, except you usually want your inline functions static. At least I rarely bother to instantiate external inline definitions. – doynax Apr 25 '15 at 20:59
  • 1
    A good read about the why and how of inline and static functions in C, is [**Inline Functions In C**](http://www.greenend.org.uk/rjk/tech/inline.html) – David C. Rankin Apr 26 '15 at 01:21
1

Assuming multiple C source files include the .h file, then inline functions in the .h file are ok, but I'm not sure why you'd want a static function in a .h file, to end up being duplicated, once for each C source file. The compiler may be smart enough to not generate code for static functions that are not called within a C source file.

rcgldr
  • 27,407
  • 3
  • 36
  • 61