extern question

Leo de Wit leo at philmds.UUCP
Sat Jul 2 18:46:30 AEST 1988


In article <4182 at pasteur.Berkeley.Edu> faustus at ic.Berkeley.EDU (Wayne A. Christopher) writes:
|Is the following acceptable C code:
|
|	extern int foo();
|
|	bar()
|	{
|		int (*bla)() = foo;	/* CASE 1 */
|
|		foo();			/* CASE 2 */
|	}
|
|	static int
|	foo()
|	{
|		...
|	}
|
|In other words, should extern be interpreted as "possible forward
|reference" (the way I'm using it), or "externally defined symbol"?  All
|the compilers I have used handle case 2 ok, but one couldn't deal with
|case 1.  Shouldn't they both work the same?
|
|From an aesthetic viewpoint, I like to use extern anywhere I declare
|something but don't define it.  I think writing
|
|	int foo();
|
|for static functions is not as clear.
|
|	Wayne

Indeed. You should write

static int foo();

This is - in my point of view - the only correct forward declaration.
It is also better from aesthetic point of view, in that it does use the
correct storage type. Notably the Lattice C compiler is rather strict
regarding storage classes / extern definitions; I like that. As for
extern declarations, they typically belong in a header file (the one
that belongs to the module defining the extern function / variable, i.e.
is the - outgoing - interface of that module to the outside world).

      Leo  (that's the way I C it; don't blame me for being stubborn 8-).



More information about the Comp.lang.c mailing list