Visibility - static


Compilation unit: the entity seen by one compiler call

  • The C file that is being compiled

  • All included header files

  • Result is usually one object file

Symbol resolution:

  • By compiler inside one compilation unit

  • By linker across multiple compilation units

    • Among symbols that the linker sees

A Somewhat Contrived Example (1)

Two compilation units linked into an executable …

#include <stdio.h>

extern float avg(
  int *begin, int *end);

int main(void)
  int array[] =
    { 1, 2, 3, 4, 5 };
    avg(array, array+3));
  return 0;
int sum(int *begin, int *end)
    int sum = 0;
    while (begin < end)
        sum += *begin++;
    return sum;
float avg(int *begin, int *end)
      (float)sum(begin, end) /

A Somewhat Contrived Example (2)

Function sum() in avg.c is globally visible

  • Anybody could declare it and use it

    • Linker will resolve it (that’s his job)

  • Name could clash with another symbol in another compilation unit

    • Linker error (“duplicate symbol”) when linking statically

    • Subtle bug when using shared libraries

  • Innocent reader has to think twice

    • “Can I modify the function without telling anybody?”

Ambiguity that needs resolution!

The static Keyword (Hooray!)

Solution: static - restrict visibility to the compilation unit

static int sum(
  int *begin, int *end)
    int sum = 0;
    while (begin < end)
        sum += *begin++;
    return sum;
  • Nobody has to think twice

  • Nobody can use sum but the file it is defined in

  • No name clashes

  • No ambiguity!

  • Only readability!

  • Compiler can automatically inline the function

  • … with only 6 characters of effort