[Info-vax] Possible C compiler static variable initialization problem.
glen herrmannsfeldt
gah at ugcs.caltech.edu
Wed Feb 26 18:01:57 EST 2014
JF Mezei <jfmezei.spamnot at vaxination.ca> wrote:
> I was under the impression that variables defined in a file outside a
> function were local to that compilation unit, but available to all
> fundtions within that compilation unit.
No.
> if you use "global" or "extern" when defining the variable, then this
> causes the variable name to be published to the linker to allow the
> variable storage to be shared between all object modules being linked
> into the executable. If I remember, global defined the storage, extern
> told the compiler that storage was defined by someone else and to let
> the linker resolve it.
No. Variables at file scope (outside any function) that are NOT
declared static are known outside. (That is, global unless declared
static, never mind that this is a misuse of the word static.)
Separately, all but one that isn't static should be extern.
The one that isn't extern gives the initial value, zero if not
specified.
The Fortran rules are slightly different, in that you can have
any number of COMMON blocks specified (which don't initialize to
a specific value) and at most one in BLOCK DATA which can initialize
the value.
More interesting when you mix Fortran and C.
-- glen
More information about the Info-vax
mailing list