In C89, does the static
keyword affect scope?
My software lead told me:
"A variable marked static at the top of a file doesn't technically have global scope any longer. Static is a scope qualifier as well as a storage keyword. Scope is a concept that covers visibility of symbols, though visibility is automatically compiled to have storage duration intrinsically tied in by almost all languages. By this I mean that you can't name a scope that doesn't also define the storage duration in C/C++. Expression scope is not user defined and in C/C++ covered by l-param and r-param Block scope is fully lexical in C/C++ by user defined bodies Function scope is fully lexical in C/C++ by user defined bodies and declarations File scope does not technically exist in C/C++, as globals and module scope take over depending upon lexicon Module scope is keyword defined using static in C/C++, other scope lexicon change the rules for access but the visibility remains module based Global scope is the default in C/C++ when no other scope applies and is lexically controlled by the extern keyword The issue is that static is not JUST a scope qualifier as a keyword. It is a scope qualifier AND a memory keyword."
I'm confused. I've always thought that static relates to the visibility between translation units and the storage duration of the variable. Both of which are unrelated to scope. Is this not the case? Is the static/scope relationship different in C++?
A variable marked static at the top of a file doesn't technically have global scope any longer.
"Global scope" is not a concept that exists in C. The proper term is file scope. In C++, a similar concept exists called the global namespace. It seems that overtime people combined the two terms.
Static is a scope qualifier as well as a storage keyword.
static
is not a scope qualifier, it is a storage-class specifier. static
can affect linkage and storage duration, but not scope.
Scope is a concept that covers visibility of symbols, though visibility is automatically compiled to have storage duration intrinsically tied in by almost all languages.
Scope has nothing to do with visibility of symbols (in the linker sense). Linkage does (hence why it's called linkage). The second clause is gibberish.
By this I mean that you can't name a scope that doesn't also define the storage duration in C/C++.
This sentence also doesn't make sense. Consider a local static variable at block scope. It has static storage duration even though block scope defines automatic storage variables.
Expression scope is not user defined and in C/C++ covered by l-param and r-param
"Expression scope" makes no sense. "l-param" and "r-param are also meaningless words.
Skipping the part about "lexical" and "modules" because it makes zero sense.
The issue is that static is not JUST a scope qualifier as a keyword. It is a scope qualifier AND a memory keyword.
Again, static
has nothing to do with scope or memory. Using this oversimplified explanation leaves out basically all other aspects of storage duration, scope and initialization so it just plain doesn't work.
Section 6.2.1 of the C11 standard defines what "scope" means:
For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces. There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
Section 3.1.2.1 of the C89/90 spec is almost identical:
An identifier is visible (i.e., can be used) only within a region of program text called its scope . There are four kinds of scopes: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its parameters.)
So there is no such thing as global scope, at least as far as the C standard is concerned. An identifier defined outside of any function or block has file scope, and the presence or absence of static
has no effect on that, only on the symbol's linkage, which is something completely different (but which your lead may be conflating or confusing with the term "scope").
Your informant is confused. static
has no impact on scope whatsoever.
File scope is a misnomer because you can construct multifile translation units using #include directives or other hypothetical implementation-dependent facilities. Global scope is also a misnomer because a program can be made up of multiple translation units. Modules are still not part of the language.
static
can affect linkage, but that is a different concept to scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With