1

I know that static function's name is visible only within the file (translation unit) in which it's declared. This makes encapsulation possible.

But static function is usually declared in the source file because if you do it in the header file, you can end up with multiple implementations of it (which I think is not the intention of static).

Example:

main.c

#include "functions.h"

int main()
{
    FunctionA();
    FunctionB(); // Can't call regardless of "static".
    return 0;
}

functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void FunctionA();

#endif /* FUNCTIONS_H */

functions.c

#include "functions.h"
#include <stdio.h>

static void FunctionB(); // Same whether "static" is used or not.

void FunctionA()
{
    printf("A");
}

void FunctionB()
{
    printf("B");
}

So when is static useful?

takfuruya
  • 2,453
  • 2
  • 15
  • 10
  • 2
    it's a rough equivalent to a `private` method in OOP - sometimes you DON'T want an internal-only function being usable by anyone else EXCEPT your own library. – Marc B Dec 05 '13 at 20:47
  • 1
    To see the purpose you need to have more than one .c file. Implement a function called `private_helper()` in both and see what happens. –  Dec 05 '13 at 20:48
  • 1
    When you want to call FunctionB from FunctionA in functions.c. `static` is for not-exported functions. – Guido Dec 05 '13 at 20:57

3 Answers3

5

static says the function has internal linkage. This means it will not be linked with other uses of the same identifier in other files (translation units).

For example, suppose in Tree.c I have a function that operates on Tree structures, and I have some local subroutine called UpdateNode that operates on part of the Tree. Further suppose that in List.c, I have a function that operates on List structures, and it also has some local subroutine called UpdateNode that is just for List structures, not for Tree structures.

If I left both of these subroutines with external linkage, the linker would complain about multiple definitions. By marking them with internal linkage with static, this problem is avoided.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

It is useful when:

  1. The function should not be visible outside the translation unit.
  2. Visibility of the function should be considered "hidden" (when dynamic linker is used).
  3. To have compiler warn about unused function.
  4. Have trivial functions be inlined, in which case static function is defined in the header. A better alternative to macros for pre-C99 code.

Or a combination of any of the above. There might be something else that I didn't immediately think about.

0

In additional to all answers I would like to add what MISRA C says about static keyword.

Rule 8.8 The static storage class specifier shall be used in all declarations of objects and functions that have internal linkage

The Standard states that if an object or function is declared with the extern storage class specifier and another declaration of the object or function is already visible, the linkage is that specified by the earlier declaration. This can be confusing because it might be expected that the extern storage class specifier creates external linkage. The static storage class specifier shall therefore be consistently applied to objects and functions with internal linkage.

And this one

Rule 8.10 An inline function shall be declared with the static storage class

If an inline function is declared with external linkage but not defined in the same translation unit, the behaviour is undefined.

In other words, use static keyword wherever it's possible

Community
  • 1
  • 1
Deck
  • 1,969
  • 4
  • 20
  • 41