Why won't extern link to a static variable?
Asked Answered
I

5

72

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?

Ichneumon answered 15/5, 2010 at 21:8 Comment(0)
M
148

The 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 its job in preventing a connection from an extern.

Keep in mind that there are four flavors of file-scope variable definition:

  1. 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.
  2. extern int blah; — blah must be defined elsewhere and is referenced from this file.
  3. int blah; — This is the moral equivalent of FORTRAN COMMON. You can have any number of these in files, and they are all resolved by the linker to one shared int. (*)
  4. static int blah; (optionally with an initializer) — 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 declare static 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.

(*): for those of you not familiar: in the usual pattern, one compilation unit has to define a global variable, and others can reference it. It 'lives' in that compilation unit. In case (3), above, no file (or all the files) defines it. If two files say int blah = 0;, the linker will complain of multiple definitions. If two files say int blah; the linker cheerfully creates a single global int and causes all the code to refer to it.

Melissiamelita answered 15/5, 2010 at 21:10 Comment(8)
what then would 'int n' be called (in file scope), in terms of storage specifiers?Ichneumon
@bmargulies: At file scope, you mean? Because at function scope, a static var is one that retains its value after the functions returns, and at class scope, a static member has a single instance available to all objects.Tanny
@Jared -- I call it 'common'. In my recollection, the language does not define a keyword for making this explicit. If I'm wrong, I trust that someone would correct me.Melissiamelita
@bmargulies: OK, I thought in C++ terms instead of C. Thx.Tanny
hehe. You "clarified" by pointing out an analogous fortran construct. I don't think many SO users have messed with Fortran. :)Maletta
@Maletta Winking at the adults in the audience?Melissiamelita
I like the "blah" convention so much more than "foo" and "bar".Cheapen
The 3. no longer works in new versions of GCC. It gives errors. To revert old behaviour the -fcommon flag must be specified.Pontianak
C
7

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.

Coexist answered 15/5, 2010 at 21:21 Comment(0)
F
2

According to MSDN documentation:

When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

Static (C++) on MSDN: Archived in January 2015; see also the latest documentation: static § Storage classes (C++) | Microsoft Docs

Fronia answered 25/6, 2013 at 21:44 Comment(0)
N
1

iv.c:2:1: error: multiple storage classes in declaration specifiers extern static int i; ^

That is what we get on attempting to extern a static variable. Declaring extern static int i; - is analogous to the declaration float int i; You can't have float and int appear in the same declaration right? Similarly, you can't have extern and static in the same declaration.

Nahshon answered 17/12, 2015 at 4:41 Comment(0)
R
0

BTW. This is some legal C program:

static int ok;
extern int ok;

int
main(void)
{
}

But this is illegal:

extern int error;
static int error;

int
main(void)
{
}
Rob answered 15/12, 2023 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.