DEV Community

Shaw
Shaw

Posted on

static has only one meaning

static can be confusing with the wrong definition. Here's a simplified example:

The task is to write a counter function that takes no arguments and returns a different number each time, starting at 0 and adding 1 each time.

int current_counter = 0;
int counter(void) {
    return current_counter++;
}
Enter fullscreen mode Exit fullscreen mode

Now imagine someone is using this library. They write a function called counter just for testing. But when they try to link their code, it fails, telling them that counter is being redefined. But no worry, We are C programmers and we have static at our disposal!

Many believe static to have multiple meanings in C.

  • static functions are not exported globally
  • static local variables are hidden globals
  • static global variables are not exported globally What could have caused this mess of keyword overloading?

The answer lies in a shift of perspective. static means compilation unit local. A compilation unit in C is just the files given to the c compiler (gcc, clang, msvc, etc) and what they #include.

Using this definition we can say the following.

  • static functions are owned by the compilation unit
  • static global variables are owned by the compilation unit
  • static local variables are owned by the compilation unit

Using this knowledge, we can rewrite the counter function to not leak counter into the API.

First, move current_counter into a static in counter

int counter(void) {
    static int current_counter = 0;
    return current_counter++;
} 
Enter fullscreen mode Exit fullscreen mode

This will make current_counter not collide with anything else named current.

Next is to make counter a static function

static int counter(void) {
    static int current_counter = 0;
    return current_counter++;
}
Enter fullscreen mode Exit fullscreen mode

This will make counter not visible to files not in the same compilation unit.

Now things are fixed! The counter function and current_counter variable cannot be used in files not in the same compilation unit.

I hope this helped someone.

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

static means compilation unit local.

The correct term is translation unit. Hence, in C, static actually means primarily:

  • Is local to the translation unit (strictly speaking, has "internal linkage").

If it's a static variable (as opposed to a function), then it also means:

  • Is automatically initialized to 0 (or equivalent) prior to the first statement in main().
  • Persists for the entire duration of the program (hence, between function calls).
Collapse
 
matthewsalerno profile image
matthew-salerno

I really like this interpretation! Unfortunately I already had to learn this the hard way.
I think the additional tiered levels of local/global adds to the confusion, you ran into it yourself when saying that static global variables are not (exported) global.
C can be confusing to get into, but once you get used to it, it can be refreshingly simple. Hopefully this helps someone out!