Forward Referencing of Static Variables OK?

Robert J. Devine devine at asgb.UUCP
Fri Oct 11 05:24:11 AEST 1985


> 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??)

  It is not equivalent.  Declaring a variable as "static int abc;"
means that "abc" is not exported to the linker.  "int def;" is exported
and hence can be referenced in other files.  The default storage class
is "extern".

> 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??)

  To be pedantic, there are two types declarations that are possible
for external variables -- one that defines and one that references.
As an example, what does "extern int abc;" mean in a file?  That is,
is it referencing a definition of "int abc" from a different file?
Or is this where abc is defined?  Difference compilers do different
things in this case.  A portable (?) way of declaring variables that
are used in different files is to have one declaration be the definition.
To do this, initialize it and don't use "extern" in its declaration.

Bob



More information about the Comp.lang.c mailing list