By coincidence I stumbled over "The New C Standard" from Derek M. Jones, where he comments this sentence from the C99 standard regarding "storage duration of objects":
There are three storage durations: static, automatic, and allocated.
His comment:
One of the uses of the overworked keyword static is to denote objects that have static storage duration (there are other ways of denoting this storage duration).
The text marked bold is what irritates me. I've read this thread and this one, but it doesn't clarify it to me. Maybe my research is incomplete or I misunderstand his point...
I understand that the static
keyword is used to limit access to the file where the variable is declared, and I understand that those variables' lifetime is "unlimited" throughout the runtime of the program.
I also understand that some getter()
functions should be used to access those variables from other modules instead of tampering with extern
declarations.
And I understand the argument from @amon that
"
static
makes code untestable"
Btw, to overcome this, I've seen solutions where some construct like
#ifdef TESTING_ACTIVE
#define STATIC
#else
#define STATIC static
#endif
was introduced, having a compiler switch "-DTESTING_ACTIVE" for testing purposes.
Michael Barr says in his Embedded C Coding Standard:
The static keyword shall be used to declare all functions and variables that do not need to be visible outside of the module in which they are declared.
So... what exactly is Derek's problem with static
and what are those "other ways of denoting this storage duration" in C99?
static
(i.e. making a global variable private) and more with the idea of global variables in general. Note that a global variable has static storage duration whether or not it's declared with the wordstatic
(they would've used the keywordprivate
but the C language designers thought that would be too easy)public
andprivate
keywords for scope usage, allowingstatic
to only be used for denoting persistent objects