1

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 ?

carce-bo
  • 480
  • 2
  • 10
  • 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. – Jacob Faib May 25 '22 at 11:56
  • ...in a very small way. – DevSolar May 25 '22 at 11:57
  • 1
    If you declare the function `foo2` as `static` then it would have internal [*linkage*](https://en.cppreference.com/w/c/language/storage_duration#Linkage) and be "private" to the [translation unit](https://en.wikipedia.org/wiki/Translation_unit_(programming)) it's defined in. – Some programmer dude May 25 '22 at 11:57
  • My question is meant to apply to a general case. I was wondering wether I should take my old projects and write static in front of all these 'auxiliar' functions or just leave them as they are. – carce-bo May 25 '22 at 11:58
  • 1
    Don't fix what isn't broken. Limiting visibility is a good thing, but not worth touching a stable code base (unless you're in the process of re-touching the code anyway). – DevSolar May 25 '22 at 12:00
  • 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". – Some programmer dude May 25 '22 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. – carce-bo May 25 '22 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`. – Eric Postpischil May 25 '22 at 12:07
  • note that your example returns the result from void foo2 – stark May 25 '22 at 12:09
  • If your `foo2` function is not used anywhere else than in foo.c (IOW if it's some internal helper function local to foo.c), then you can safely (and you even should) add `static`. – Jabberwocky May 25 '22 at 12:18
  • 1
    @EricPostpischil: There is a thing or two to be said for *not* releasing a new version of code that has been stable for a long time, with the only thing in the changelog being "added `static`". Especially when you're looking at downstream clients. But as OP said he's in the process of re-touching his code anyway and it not having been in production yet, I agree, cleaning up is worth it. – DevSolar May 25 '22 at 12:21

1 Answers1

3

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.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • inlining does not have anything in common with `static`. compiler can choose to inline static or non-static functions at its discretion. – 0___________ May 25 '22 at 12:09
  • 1
    @0___________: This answer says nothing about how `static` affects inlining. It says something about how inlining all calls can affect a function declared `static`. – Eric Postpischil May 25 '22 at 12:12
  • So, depending on the compiler, compilation would be more efficient when adding the static keyword to these auxiliar functions ? – carce-bo May 25 '22 at 14:38
  • @carce-bo: Yes, it may allow a compiler to omit some unnecessary code. – Eric Postpischil May 25 '22 at 15:10