Why does extern int n
not compile when n is declared (in a different file) static int n
, but works when declared int n
? (Both of these declarations were at file scope.) Basically, why is int n
in file scope not the same as static int n
in the same scope? Is it only in relation to extern? If so, what about extern am I missing?
views:
126answers:
3The whole and entire purpose of static
is to declare that a variable is private to the source file that it is declared in. Thus, it is doing precisely it's job in preventing a connection from an extern.
Keep in mind that there are four flavors of file-scope variable definition:
int blah = 0;
blah is defined in this file and accessible from other files. Definitions in other files are duplicates and will lead to errors.extern int blah;
blah must be defined elsewhere and is referenced from this file.int blah;
This is the moral equivalent of FORTRANCOMMON
. You can have any number of these in files, and they are all resolved by the linker to one sharedint
.static int blah;
This is static. It is completely private to this file. It is not visible to externs in other files, and you can have many different files that all declarestatic TYPE blah'
, and they are all different.
For the purists in the audience: 'file' = compilation unit.
Note that static inside functions (not at file scope) are even more tightly scoped: if two functions declare static int bleh = 0;
even in the same file, they are unrelated.
An extern var is already "static" because there's only one instance of it. Declaring it as static is a redundancy.
An extern is a single instance of a var (or any other object) that's available globally; its scope ends when main()
returns.
EDIT: OK, the people commenting on my answer are right to point out that my answer is confusing. I used the word "static" thinking about static class members in C++, which isn't obvious. Sorry for that and check the answer by bmargulies :)
In standard C, there are two scopes for variables declared outside of a function. A static
variable is only visible inside the compilation unit (i.e., file) that declared it, and non-static variables are visible across the whole program. An extern
declaration says that the variable's location isn't known yet, but will be sorted out by the linker; it's compatible with non-static variables, but extern static
is just crazy talk!
Of course, in practice there are other visibilities these days. In particular, there are now scoping levels between that of a single source file and a whole program; the level of a single shared library is a useful one (settable through mechanisms like GCC function attributes). But that's just a variation on the theme of non-static variables; static
keeps the same interpretation it had before.