Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
What are disadvantages of static functions (ie functions with internal linkage) in C?
Functions in C have external linkage by default. In other words, the storage class specifier extern
is applied to functions by default, with the effect that they are visible to all translation units.
The storage class specifier static
gives functions internal linkage and restricts visibility to the given translation unit.
Some comparison to other programming languages is instructive:
- In Java, one is taught from early on to give thought to the right choice of access modifier, which in most cases means choosing between
public
andprivate
. - In Pascal, one can define functions (and procedures, ie functions without a return type) within other functions (or procedures). However in C, functions can only be defined at file scope, giving them global visibility by default (and thereby making us have to worry about internal vs external linkage).
Restricting visibility is a good thing. But I have encountered relatively few functions marked static
in production code, even though they could have been given internal linkage. We can therefore ask: What are disadvantages of static
functions (ie functions with internal linkage) in C?
An admittedly opinion-based way of asking would be: Why aren't static
functions used much more widely in C? (The answer might simply be "traditionally people don't bother to change the default linkage for functions from external to internal".)
3 answers
There are some reasons why a function would intentionally not be declared static
. (In the following I use the term 'module', but in contrast to others, I use it not to describe a file or translation unit, but rather for an architectural entity (for example, a library), which can consist of several source files / translation units. Moreover, the OP asked about functions, but the following also applies to variables in the same way.)
-
The function is intended to be part of the public interface of some module/library. Then, the function would have to be given external linkage. In such a case, the respective function would, however, be declared in some public header file. Since modules/libraries can be used without making use of all their functions, it is possible to observe situations where a function with external linkage is not used outside its own translation unit, but external linkage is nevertheless appropriate.
-
The function is not part of the public interface of some module/library, but has to be shared among different translation units of the module/library. In such a situation the function would be declared in an internal header file of the module/library that is not included directly or indirectly in the public header file. This is a scenario where the simple distinction between internal and external linkage is not sufficient. In case of shared libraries, however, there is the possibility to define which symbols shall be exported from the library. But, this concept has no standard way of being expressed in the C language.
-
A special case which is a bit of a mixture of the above two ones is, where a function is not part of the public interface, but is used by a macro or inline function that is part of the public interface. In this case, the function also needs to be given external linkage.
Some people think that making functions static
makes it impossible to call them from unit tests. It is not impossible, but requires a bit of trickery: You have to #include
the .c
file with the static
functions into the .c
file that holds the unit-test code. This trick, however, is commonly known, fully compliant with the C standard and also applied by commercial unit-testing tools. Nevertheless, this possibility may come as a surprise to many developers coming from languages with module systems.
I also seem to remember that I was once working in a development context for embedded systems where some debugger had difficulties in handling static functions properly. It is quite some time ago in my case, but certainly when you are working in an environment with a very specific tool set you may even today encounter limitations of these tools.
But, after all, yes, it happens that developers simply forget to declare functions as static
- or they may not even be familiar with the concept. I would argue that this is not uncommon: First, forgetting to declare a function static
happened to me as well :-), second, all static code analysis tools (no pun intended) that I have used had some checks to detect such situations and issue a warning to inform the developer about functions that could be declared static
. Therefore, there must be an interest in having such checks in the tools...
0 comment threads
There is basically only one reason not to use static functions in C, as opposed to functions with global scope. That's if you want to access the function from outside the module.
Otherwise, if the function is only used in the specific module, then it's beneficial to not export it to the whole world. By making it static (a bad name for "not global", but that's C) you know that the only callers to the function are local to the module.
It also means the function is only in the namespace of the module, and you don't have to worry about being unique within the global scope. For example multiple modules can have local functions called init_state without any problems.
So overall, the answer is to make functions static in C unless they are meant to be called from outside the module.
0 comment threads
Besides what Olin already said, I guess too many people were taught C using K&R book, which was great at the time, but it completely neglects modern SW engineering best practices (encapsulation, data hiding, modularization, etc.), many of which were popularized to the wider programmer audience by C++ and later by Java, since these languages support OOP and in this paradigm modularization (which static
in C is for) is essentially a must (in C++ you have namespaces, classes and all sort of mechanism to do so).
In particular, separation of the interface of a module from the implementation is essentially made using static
vs extern
functions: write the interface in a header file where you declare all the public functions the module must export, then include the header in the corresponding source file (.c
file) where you implement the public functions using all the private (static
) helper functions you need.
One of the staple of good SW design is to avoid fat interfaces, i.e. interfaces with a lot of functions that don't need to be public, but are there "just in case". C programmers that cram their headers with all the functions in their modules probably haven't been exposed to OOP languages or are too inexpert in modern C programming practices (you can do OOP even in C, but it requires quite a lot of discipline since the language won't help you there)
Another alternative is that they are "old timers" that grew up with assembly and C at low level, where legacy code written for "bare metal" systems didn't need too much SW engineering best practices to work well. After all, if all you have is an MCU with 16k or so of Flash and some kiBs of RAM (and you are a good programmer) you can write pretty good software even using C as an higher level assembly.
The problem is that this doesn't scale well for bigger systems. Once your project code base size hits the thousands of source code lines of C you are in for very nasty surprises if you don't employ higher level techniques, and one of the most basic techniques is using static
to compartmentalize functions in the module they belong to.
1 comment thread