1

I was reading about static functions in C on this thread: https://stackoverflow.com/a/558201/7997108

As i understood it, basically where you define the static functions is the only "place"/file (fileA.c i.e.) where you can call it, which kinda makes this function "private" to that .c or .h file (or translation unit). But if you #include this file in some other (fileB.c) you will still be able to use it there as well?

So im trying to understand in which case you want the function to be static to its own .c and how it makes sense if you can still use that "private"/static function by just including the file where it is defined.

Also as i understand, in case you don't include some other file where some function is defined you won't be able to use/call that function anyways right?

In other words i just cant comprehend what is the typical use-case for static functions and how is it different from non-static function basically.

Bodega
  • 19
  • 1
  • 7
  • 2
    _"But if you #include this file in some other (fileB.c) you will still be able to use it there as well?"_. Yes, and that's one op0f tbhe reasons why you don't #include .c files. – Jabberwocky Sep 23 '19 at 11:23
  • I have a trick where, when I want `static` functions that are only called once to have their own file but don't want them to have global symbols, I separate them into their own `.c` file and `#include` them from where I want to use them. – S.S. Anne Sep 23 '19 at 11:23
  • Implementing a `static` function in a header (.h) file would define a "private" function of the same name and with the same implementation in **every** translation unit that includes this header file. This will duplicate the code. If you `#include` a source file (A.c) with the definition of a `static` function in another source file (B.c) and compile both A.c and B.c you will also get duplicate copies of the same "private" function in both translation units. – Bodo Sep 23 '19 at 11:28
  • The typical organization of something like a library is that you define all the functions that represent the public interface to that library as non-static, and ALL other functions should be static. That way, library clients are forced to use only the public interface, and shouldn't know or care about implementation details. – Lee Daniel Crocker Sep 23 '19 at 20:09
  • Thank you all for answers, but one more question to @Bodo: Then what is the difference here when you have it defined in .h or .c file and then #include it, regarding the duplication of the code, is there any? – Bodega Sep 24 '19 at 06:48
  • @Bodega The compiler and preprocessor do not care if you name your file `*.c` or `*.h` or `*.something`. The normal convention is that the `*.h` files contain the declaration or the public interface and the `*.c` files contain the (private) implementation and that you normally include `*.h` files both in the corresponding implementation and the callers / users of the interface. This convention is not enforced by the compiler, but build systems may rely on this. – Bodo Sep 24 '19 at 07:47

2 Answers2

6

Imagine you're coding a library and you need to define a little helper function. Maybe you name this function test:

int test(int x)
{
    return x > 100;
}

This function isn't part of the public interface, so you leave it tucked away inside the .c file. And everything is good right?

Wrong.

Problem #1: Anyone can use this function simply by adding this declaration to their code:

int test(int);

Problem #2: Imagine if another library had their own test helper function. If that library and your library were used in the same program, the linker would error because two functions can't have the same name.

The solution is to use static. With static, the functions are unique to their translation unit and aren't exposed globally.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • Hmm ok, i think i understand, i thought that you cant use that function unless file is #included. Would anything be different in your scenario if this test() was declared and defined in .h file? – Bodega Sep 23 '19 at 11:31
  • A `static` function in a header file would be recompiled every time that header is included. That's not a good thing - it's wasteful! – Pubby Sep 23 '19 at 11:36
  • It's not the compiler that issues the error, but the linker. Would you like to improve this explanation? – harper Sep 23 '19 at 11:51
  • @Pubby Having a function in a header file should be avoided. But it works, and it can be handy to have some small `static inline` functions that won't get a public visible name. – harper Sep 23 '19 at 11:53
  • @Pubby but would that change anything regarding the problem #1&2 ? – Bodega Sep 23 '19 at 11:58
  • If you put it in the header then it's not private anymore. It's part of the interface. – Pubby Sep 23 '19 at 11:59
  • @Pubby Does it make any sense to define static function in header file ever? Especially if this function isn't 'small' by any means either. – Bodega Sep 23 '19 at 12:08
  • You will do well if you never put static functions in headers. – Pubby Sep 23 '19 at 12:11
1

Static functions in C usually used to separate an interface and its implementation. They play role of implementations that are not visible to clients of interfaces. On the other hand non-static functions play role of interfaces and are visible to clients of the functions.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335