[comp.lang.c] Yet more on .headers

vlcek@mit-caf.MIT.EDU (Jim Vlcek) (07/13/88)

In article <1049@ficc.UUCP> peter@ficc.UUCP (Peter da Silva) writes:
>What does your compiler do when you say this?
>
>	extern int foo;
>	...
>	int foo = 10;
>
>Mine tells me I'm trying to initialise an extern and refuses to compile it.

>-- `-_-' Peter (have you hugged your wolf today) da Silva.

If that is indeed the case, I think your compiler is broken (big time,
at that).  Such a sequence is definitely legal in K&R C (see p. 77),
and will allow references to variables to precede their definitions
within the source file.

When I first started programming in C, I worried about just this sort
of thing -- I thought I would get into trouble by having an extern
declaration in the same file as a definition.  This led me to
(briefly) remove #include <foo.h> from all of my files foo.c.  That
obviously loses big time, because there is much more than just extern
declarations in each <foo.h>, so I read the good book and was set back
on the straight and narrow path.

Many, if not most, compilers will also accept what looks like two
definitions of the same variable in two separate source modules:

foo1.c:

  #include <stdio.h>
  int foo = 1;
  main() {
  }

foo2.c:

  #include <stdio.h>
  int foo;
  not_main() {
  }

This works so long as there is only one initialization of foo; that is
taken as the definition of foo.  I don't use this, it seems to me a
hack to rescue old code, and slightly inconsistent with C coding
rules.  It works on the 4.3BSD compilers I use here at MIT.
-- 
Jim Vlcek
vlcek@caf.mit.edu
!{ihnp4,harvard,seismo,rutgers}!mit-eddie!mit-caf!vlcek