I get why static local variables are called "static" -- we want them to be allocated in static memory! But what is the reason for calling functions and variables we want restricted to the current file "static"? I don't see the connection; either way, they're allocated in static memory, right?
-
1"we want them to be allocated in static memory" any expectation/assumption regarding where a variable is actually stored should not drive how you write your code. Keywords are at best a suggestion in this area. Register in C is a good example. Just because you declare a variable "register" does not mean it will be stored there. It could be there aren't enough registers or the compiler could just ignore it. – cdkMoose Oct 21 '16 at 17:00
-
What is "static memory"? Do you mean the BSS and DATA segments of a ELF file? – gardenhead Oct 21 '16 at 17:12
-
1The short answer is: it is a real mess. Many different languages use `static` to mean many different things, and many of those languages have inconsistent meanings even within the language. In most cases there's some way to say "the characteristics of this thing are determined through a *static analysis* of the code" -- that is, some characteristic is determined by the compiler at compile time. Like, the address of a static variable is determined by the compiler, unlike the address of a heap or stack allocation. – Eric Lippert Oct 21 '16 at 17:46
-
@EricLippert This is what I was looking for. Please post as an answer. Also: how does the "static analysis" interpretation apply to the "storage duration" meaning of static? – Elliot Gorokhovsky Oct 30 '16 at 17:42
-
Because you can statically determine the lifetime of the storage. Unlike a local variable, whose lifetime depends on the duration of the call or the lifetime of its enclosing closure. – Eric Lippert Oct 31 '16 at 04:42
-
@EricLippert and that duration would just be "forever" right? – Elliot Gorokhovsky Oct 31 '16 at 04:42
3 Answers
The static
keyword is overloaded with multiple meanings, and which meaning applies depends on where it appears. It affects both the storage duration (lifetime) of an object and the linkage of the identifier associated with an object of function declared at file scope.
Refer to "6.2.2 Linkages of Identifiers" and "6.2.4 Storage Duration of Objects" in the online draft of the C11 standard for details.
Note that the static
keyword only affects linkage for function and object identifiers declared at file scope (outside the body of any function) - identifiers declared within a function or block (and without the extern
keyword) have no linkage.

- 10,826
- 1
- 31
- 43
I get why static local variables are called "static" -- we want them to be allocated in static memory
Um, no. The static
keyword dates back to the early days of the C programming language, where it can both specify the scope and lifetime of the item.
Languages "inspired" by C have copied the static
keyword and used it with its "lifetime" meaning: static members typically have a lifetime of the entire execution of the application.

- 38,972
- 9
- 88
- 121
In C, and depending on use, the static
and extern
keywords denote either the scope or the lifetime of a variable
IMHO, this was (and continues to be) a mistake. It seems illogical that use of static
on a variable at file-scope has a different meaning than when used on a function-scope variable.
At least C++ has addressed this, by using public
and private
for scope, while retaining static
for lifetime.

- 2,018
- 2
- 16
- 27