Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA
Path: utzoo!watmath!clyde!cbosgd!ihnp4!mhuxn!mhuxj!mhuxr!ulysses!allegra!mit-eddie!godot!harvard!seismo!brl-tgr!gwyn
From: gwyn@brl-tgr.ARPA (Doug Gwyn )
Newsgroups: net.lang.c
Subject: Re: Lattice/UNIX incompatibility
Message-ID: <6907@brl-tgr.ARPA>
Date: Wed, 2-Jan-85 13:15:44 EST
Article-I.D.: brl-tgr.6907
Posted: Wed Jan  2 13:15:44 1985
Date-Received: Fri, 4-Jan-85 04:48:29 EST
References: <3220@alice.UUCP>
Organization: Ballistic Research Lab
Lines: 52

> 	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*/
	}