Forward Referencing of Static Variables OK?

Lowell Savage savage at ssc-vax.UUCP
Tue Oct 8 11:21:10 AEST 1985

> A C compiler we are using was designed in such a way that the following
> code would not compile:

> 	extern int thing;	/* don't know yet if thing is static or not */
> 	main() {...}
> 	static int thing = 0;	/* now we know that it's static */
> 	int increment(x)...
> Now the situation is basically this:  the compiler complains because 
> "thing" is first declared external int, referenced (as extern int) and
> then declared (the designers say "re-declared") static int after the
> reference.
> Their contention that not allowing this type of forward referencing of
> static variables is consistent with "The C Programming Language", (K. & P.).

> What say you all?  Is there any language in K & P which would dissuade them?
> Thanks!
>                --Jim Prouty	Tektronix, Inc

>From the way I read K&R, the "static" keyword is redundant, unless
it is inside a function.  In other words, "static int thing;" is
equivalent to "int thing;" unless the declaration takes place inside
a function.  (Any comment on that??)

Then, the "extern" keyword specifies that the variable declared is
contained in some other program unit (usually outside the current
function, but in this case, outside the current file).  I have seen
nothing in K&R that says that this variable can be redeclared inside
the current program unit.  In other words, if the compiler writer
wants to be nice, he will let you redeclare it, but he/she does
not have to.  (Any further comments??)

Personally, I would like to be able to do this myself on occasion.
For instance I would like to put the "extern" declaration in an
include file which is included in several source files, and then
"redeclare" it in one of them.  Also, I'd like to have the compiler
then make sure that the two declarations are compatible (so I don't
declare "thing" as a long int in the include file, and as a char in
the code.)  But what we really needed from K&R is a method of declaring
variables outside of functions in include files so that these variables
only need to be declared once.  For instance, if file a.h has the
declaration "int thing;", and a.h is included by b.c and c.c, does
b.c access the same "thing" that c.c does?  I don't think that K&R
specify.  And if the declaration is "extern int thing" then there must
still be a further declaration of "thing" somewhere else.  (Anybody
got anything else to say???)

Sorry it got so long.

				There's more than one way to be savage,

				Lowell Savage

More information about the Comp.lang.c mailing list