2

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.

asked Sep 23, 2019 at 10:55
6
  • 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. Commented Sep 23, 2019 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. Commented Sep 23, 2019 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. Commented Sep 23, 2019 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. Commented Sep 23, 2019 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? Commented Sep 24, 2019 at 6:48

2 Answers 2

10

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.

answered Sep 23, 2019 at 11:22
Sign up to request clarification or add additional context in comments.

8 Comments

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?
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!
It's not the compiler that issues the error, but the linker. Would you like to improve this explanation?
@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.
@Pubby but would that change anything regarding the problem #1&2 ?
|
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.

answered Sep 23, 2019 at 11:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.