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.
Comments on What are disadvantages of static functions (ie functions with internal linkage) in C?
Post
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".)
1 comment thread