Proper use of "extern" ...

Larry Cipriani lvc at danews.UUCP
Tue Jun 10 06:03:54 AEST 1986


My understanding of extern is that it simply declares the
object in question.  That is, the compiler is told what
type the object is.  I don't know of any other use for extern.

When you define the object, you tell the compiler the
type, and allocate space for it, etc.

> A file has two functions in it, "f" and "g", thus
> 
> 	static void f()
> 	{
> 		g();
> 	}
> 
> 	static void g()
> 	{
> 	}
> 
> f uses g and g is declared after f (I know we can put g before
> f and the following is no longer applicable but ...).
>
> At the top of this file do we put
> a) static void g() 	or
> b) extern void g()?	(extern static must be meaningless).

Again, I don't mean to be picky, but f and g are "defined" here,
not "declared".  Any book that doesn't follow this convention is wrong.

> My understanding (this varies with the book you read) of "extern" is that
> it means the object is declared "later in this file or in another file" in
> which case I vote for (b). Is this right?

Yes, but for the wrong reason.

I often put ALL of the declarations in one header file, and include
it in all the other files.  This is a matter of style, and I recognize
its weaknesses, but I like it better than all the alternatives.  By the
way, extra, redundant, or unnecessary externs are allowed and don't matter,
at least on my C compiler.
-- 

Larry Cipriani		AT&T Network Systems
danews!lvc		"Nothing is worse than an itch you can never scratch."



More information about the Comp.lang.c mailing list