Forward Referencing of Static Variables OK?

VLD/VMB gwyn at BRL.ARPA
Sun Oct 6 11:16:57 AEST 1985


Actually, I no longer remember why I thought that

extern int thing;
...
static int thing = 0;

is legal (according to X3J11).  My reading today is
that the first declaration gives "thing" external
linkage and the second declaration gives it static
linkage, which is a contradictory declaration.  I
think I must have missed the description of linkage
in the X3J11 document when I first looked this up.
My current position is that this is NOT legal usage.
Sorry for the mistake; thanks for pointing it out.

Minow sent me another illustration:

func() {
	extern char *foo();
	...
}
...
static char *foo() { ... }

which my latest attempt at understanding X3J11
tells me is illegal, since without a file-scope
definition in effect, the "extern" forces external
linkage whereas the "static" specifies static
linkage.  In this case, adding

static char *foo();

at the front of the file will keep the "extern"
from having an effect on the linkage type.

K&R is not nearly as thorough in this whole area
as the X3J11 specification, although X3J11 seems
to be compatible with the intent of K&R.

Needless to say, there are a number of compilers
around today that do not enforce these rules.

Programmers:  Declare everything before you use it.
If it is a file-static (private) object/function,
call it "static" whenever you declare it.  If it is
an external (public) object/function, not defined in
the same file, call it "extern".  If it is external
but defined in the same file, you may call it
"extern" in all declarations other than the defining
one.  Don't call anything both "extern" and "static".
(X3J11 is actually more complex than this, but these
rules should keep you out of trouble.)



More information about the Comp.lang.c mailing list