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;…


This content originally appeared on DEV Community and was authored by Shaw

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++;
}

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++;
} 

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++;
}

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.


This content originally appeared on DEV Community and was authored by Shaw


Print Share Comment Cite Upload Translate Updates
APA

Shaw | Sciencx (2021-12-09T18:17:54+00:00) static has only one meaning. Retrieved from https://www.scien.cx/2021/12/09/static-has-only-one-meaning/

MLA
" » static has only one meaning." Shaw | Sciencx - Thursday December 9, 2021, https://www.scien.cx/2021/12/09/static-has-only-one-meaning/
HARVARD
Shaw | Sciencx Thursday December 9, 2021 » static has only one meaning., viewed ,<https://www.scien.cx/2021/12/09/static-has-only-one-meaning/>
VANCOUVER
Shaw | Sciencx - » static has only one meaning. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/09/static-has-only-one-meaning/
CHICAGO
" » static has only one meaning." Shaw | Sciencx - Accessed . https://www.scien.cx/2021/12/09/static-has-only-one-meaning/
IEEE
" » static has only one meaning." Shaw | Sciencx [Online]. Available: https://www.scien.cx/2021/12/09/static-has-only-one-meaning/. [Accessed: ]
rf:citation
» static has only one meaning | Shaw | Sciencx | https://www.scien.cx/2021/12/09/static-has-only-one-meaning/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.