Redeclaration of Variables extern/static
Ian Lance Taylor
ian at airs.UUCP
Tue Apr 9 14:18:57 AEST 1991
In article <^R+_H3- at uzi-9mm.fulcrum.bt.co.uk> igb at fulcrum.bt.co.uk (Ian G Batten) writes:
>C News, amongst other things, has declarations of ``extern int foo;'' in
>either a header or towards the top of a file, and later has ``static int
>foo;''.
>
>Can anyone quote chapter and verse as to the legality of this?
The ANSI C standard 3.1.2.2:
``...
If the declaration of a file scope identifier for an object or a
function contains the storage-class specifier *static*, the identifier
has internal linkage.
If the declaration of an identifier for an object or a function
contains the storage-class specifier *extern*, the identifier has the
same linkage as any visible declaration of the identifier with file
scope. If there is no visible declaration with file scope, the
identifier has external linkage.
...
If, within a translation unit, the same identifier appears with both
internal and external linkage, the behavior is undefined.''
So in the case you mention, the behavior is undefined. This means
that according to the standard, the compiler can do anything it
pleases, specifically including ``terminating a translation.'' Of
course, in this particular case that's pretty tacky. Still, in the
long run it would probably be better to change the code (I have no
idea what that would involve).
--
Ian Taylor airs!ian at uunet.uu.net uunet!airs!ian
First person to identify this quote wins a free e-mail message:
``But this machine chews all kinds of paper into a thick porridge that no
chemist in the world, however skilled a criminal he may be, can read.''
More information about the Comp.lang.c
mailing list