[comp.lang.c] External variable declarations

gea@Iago.Caltech.EDU (Gary Ansok) (06/26/87)

At least according to K&R (p. 77), there is no need for fancy #define's to
make sure that global variables are declared and initialized properly.

header.h:

struct foo { ... };

extern int var1;
extern struct foo var2;


file.c:

#include "header.h"

int var1 = 42;
struct foo var2 = { ... };

The disadvantage is that the type of a variable is duplicated in two places
(some mismatches will be detected by the compiler); however, you get cleaner
(and easier to write and understand) code which is, I think, less easy to
misdeclare (what happens if two or zero modules define __GLOB__? -- not that
this is damnfoolproof).  You can also spread the initializations around among
modules if you think of a global variable as "owned" by one module and "used"
by other modules (if your program works that way).

Like many other things in programming, especially C programming, this is
something where we can argue for hours.  But I thought I'd present a case
with no messy #defines or #ifs.

	Gary Ansok
	gea@romeo.caltech.edu   -- or --   ansok@scivax.arpa