Yet more on .headers
Jim Vlcek
vlcek at mit-caf.MIT.EDU
Wed Jul 13 12:44:46 AEST 1988
In article <1049 at ficc.UUCP> peter at 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 at caf.mit.edu
!{ihnp4,harvard,seismo,rutgers}!mit-eddie!mit-caf!vlcek
More information about the Comp.lang.c
mailing list