3.7.2 External Object Definitions

Stanley Friesen sarima at tdatirv.UUCP
Thu Feb 7 08:24:19 AEST 1991


In article <eyal.665840220 at echo> eyal at echo.canberra.edu.au (Eyal Lebedinsky) writes:
>I tried gnu (1.39) which I hope is ANSI by now. It likes this one:
>
>(1)	extern int i7;
>(2)	static int i7 = 1;

Well, then it is wrong.  (1) is a tentative definition with *external* linkage.
As such it is incompatible with (2), which is illegal in the same scope.
[(1) is external by 3.1.2.2 paragraph 4, the incompatibility is required
by paragraph 7].

>It hates this one:
>
>	static int i7;		/* tentative definition, internal linkage */
>(3)	extern int i7 = 1;

Beep, Gnu is wrong again!  This is actually the correct way to do it.
(3) could also be:
	static int i7 = 1;
The original (3) is legal due to the statement that makes (1) external.
In essence, if an 'extern' appears *after* a 'static' it is a backwards
reference to the 'static' declaration, otherwise it is an external declaration.
The prior declaration is clearly a tentative definition of a variable with
internal linakage by 3.7.2 paragraph 2.

The examples in 3.7.2 actually support this treatment if read carefully.

>Microsoft 5.1 likes both, but I wouldn't trust it much.

Well, at least it is only half wrong.  It is *required* to diagnose (2)
if it follows (1).

>Q: what should the comment on the right say for (1,2,3) in the sense on
>the examples at the end of 3.7.2?

(1) /* tentative definition, external linkage */
(2) /* undefined - linkage disagreement, would be internal linkage */
(3) /* definition, refers to previous (internal linkage) */


Sigh, well, I guess gcc isn't as ANSI as it likes to claim.
-- 
---------------
uunet!tdatirv!sarima				(Stanley Friesen)



More information about the Comp.std.c mailing list