global declarations

Fred Fish fnf at unisoft.UUCP
Sun Jan 6 11:40:18 AEST 1985


>> i have done both of what lauren & yao suggested. 
>> have a file called global.h which contains:
>>
>>	#ifndef	GLOBAL
>>	#define	GLOBAL	extern
>>	#endif
>>	GLOBAL	int	foo;
>>	GLOBAL	char	bar;
>>

How about in global.h (included by all files):

	#ifdef MAKE_DEFINITIONS
	#  define GLOBAL(type,name,init)  type name = init
	#else
	#  define GLOBAL(type,name,init)  extern type name
	#endif
	GLOBAL (int, foo, 0);
	GLOBAL (char, bar, '\000');

>>  all files include global.h.
>>  from here there are two ways to go about things.
>>  1) a file global.c contains:
>>
>>	#define	GLOBAL
>>	#include "global.h"	/* optional */
>>	int	foo = 1;
>>	char	bar;
>>

Then global.c is:

	#define MAKE_DEFINITIONS
	#include "global.h"	/* mandatory */


=======================
This has the following advantages:

	(1)	It encourages explicit types and initializers for
		all globals.

	(2)	Types and names are now located in only one file,
		not two that have to be kept synchronized.

	(3)	The behavior can be easily modified by simply changing
		the GLOBAL macro.



More information about the Comp.lang.c mailing list