4

I know about the static keyword in C before a function declaration to only allow the file it is declared at to use the function (the compilation unit, to be more precise). I've done some research and I have found already answered questions regarding including them in headers, and what it does in detail, but I have one that I think is missing:
Does the static keyword add anything to a program, when there's no other declaration of a function but the one followed by its definition ?
e.g. :
main.c

#include "foo.h"

int main () {
    foo();
    return 0;
}

foo.c

void foo2() {
   return ;
}

void foo() {
    return foo2();
}

foo.h

void foo();

What would be the difference if foo2() was declared as static void ?

11
  • 1
    I suppose if you were building an application as a library, without static the compiler might not be able to prove that foo2() isn't externally visible (via some header), so would have to export the symbol. This would increase the size of the resultant binary. Commented May 25, 2022 at 11:56
  • 1
    If you declare the function foo2 as static then it would have internal linkage and be "private" to the translation unit it's defined in. Commented May 25, 2022 at 11:57
  • 2
    If they're not supposed to be called from any other translation unit, then making the functions static would be a good idea. But also heed the advice of @DevSolar about "not fixing what isn't broken". Commented May 25, 2022 at 12:00
  • 2
    I am indeed in the process of re-touching it because it is about to go into production. I'll take the risk and add a couple static keywords here and there, see what tests have to say. Commented May 25, 2022 at 12:04
  • 3
    Ignore the advice not to touch old code. This leads to code rot and perpetuation of bad code. It is good to update bad code and keep it fresh and well maintained, including limiting visibility by adding static. Commented May 25, 2022 at 12:07

1 Answer 1

4

When we have an application written by multiple people, and one person defined a function square to compute the mathematical square of a number and another person defined a function square to draw a square on a display, then, if neither function is defined with static, linking the program may result in a multiple-definition error. When the functions are defined with static, this multiple-definition error will not occur, because the same name will not be exported from multiple object files.

If there is only one definition of the name in the entire program, then no such multiple-definition error should occur. However, if the function is declared static, the compiler knows it does not have to export a definition. This means that, if the compiler “inlines” the function into all the locations that call it, it does not have to emit assembly code for a separate implementation of the function. (Inlining a function is the process of incorporating its code into the place where it is called, instead of using a call instruction to call the function.)

Sign up to request clarification or add additional context in comments.

4 Comments

inlining does not have anything in common with static. compiler can choose to inline static or non-static functions at its discretion.
@0___________: This answer says nothing about how static affects inlining. It says something about how inlining all calls can affect a function declared static.
So, depending on the compiler, compilation would be more efficient when adding the static keyword to these auxiliar functions ?
@carce-bo: Yes, it may allow a compiler to omit some unnecessary code.

Your Answer

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.