[comp.lang.c] Forward declarations

karl@haddock.UUCP (Karl Heuer) (01/12/87)

About a month ago I pointed out that the three keywords "static", "extern",
and "" (the empty string (not really a keyword, of course) denoting the lack
of a storage-class specifier) only cover three of the four elements in the
Cartesian product of { local, global } with { declare, define }, and I asked
whether there were a portable way to make a forward declaration of a static
object.  (I've been using "extern" for this, but I wondered if that really
works on all compilers.)

One e-mail reply pointed out that a one-pass compiler might output linker
information immediately, which would then go unresolved at load time.  I think
a recent posting on another subject also mentioned the possibility that, in
the code fragment "extern int x; int *y = &x; static int x;" (which is what I
am currently using) the interpretation may be (perhaps even should be) that
there is a global x in some other file (distinct from the local one in this
file).

If anyone uses or knows of such a braindamaged compiler, I'd be interested in
knowing how one *does* achieve a forward declaration using it.

Someone asked why I don't just move the definition to the top of the file.  In
the first place, this discussion applies (to a lesser extent) to functions as
well as variables, and although I often use that bottom-up style voluntarily I
don't want to be forced into it.  In the second place, the suggestion doesn't
handle the recursive case -- which can occur with data as well:
	static struct Linkedlist x = { &y, ... };
	static struct Linkedlist y = { &x, ... };

Tony Hansen said he believes ANSI has blessed this use of "extern".  My copy
(May86) is a bit muddy on this point, but they talk about something new they
call a "tentative definition" (which is either a declaration or a definition,
depending on future context); unfortunately, I want something that will work
on current compilers too.

Does the current proposal explicitly allow "extern int x; ... static int x;"
with the intended semantics?

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
(If K&R had made three independent keywords, "static", "global", and "define",
the resulting language would have been simpler, despite having more words!)