Lattice/UNIX incompatibility

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Thu Jan 3 04:15:44 AEST 1985


> 	3. Common sense indicates that the definition of
> 	an external variable should appear only once in the
> 	program text, so that its type can be changed by
> 	only altering one thing.  The logical place
> 	for such a single definition is in an include file.
> 	However, under the restrictive definition of C,
> 	this is impossible: the program breaks whether the
> 	include file says "extern" or not.

No!  #include files should be used for extern DECLARATIONS.
The data/function DEFINITIONS, as observed, reasonably
appear in only one place, preferably in the source file
that handles the corresponding data.  (In repackaging of
older UNIX code such as the Bourne shell, people found it
more convenient to lump all extern data into a separate
file by itself, e.g. "data.c".)

Example (compressed):

	cc -o example main.c stack.c

contents of file "stack.h":

	extern int level;
	extern void push(double), reset(void);
	extern double pop(void);

contents of file "stack.c":

	#include "stack.h"
	#define MAXLEVEL 100
	int level = 0;	/* NOTE: This is NOT A CONFLICT. */
	static double stack[MAXLEVEL];
	void push(x) double x;	/* ANSI: is "double x;" necessary?? */
	{	if (level < MAXLEVEL) stack[level++] = x;	}
	void reset()
	{	level = 0;	}
	double pop()
	{	if (level > 0) return stack[--level];	}

contents of file "main.c":

	#include "stack.h"
	/*ARGSUSED*/ main(argc, argv) char **argv;
	{	for (;;)
		{	push(3.14159);
			if (level % 3 == 0)
				(void)pop();
			if (level == 10)
				return 0;
		}	/*NOTREACHED*/
	}



More information about the Comp.lang.c mailing list