global data in C programming...

Maarten Litmaath maart at cs.vu.nl
Fri Mar 18 09:40:28 AEST 1988


In article <7506 at ncoast.UUCP> btb at ncoast.UUCP (Brad Banko) writes:
\...

\x.h:
\	int i,j;
\
\x.c:
\#include "x.h"
\
\xyz() {
\	i = ...
\}
\
\y.c:
\#include "x.h"
\
\zzt() {
\	i = j ...
\}

\...

You have to reserve space for the variables only in one file, in the
other files you must declare the variables "extern", so the following
is a beautiful solution (thanks to Paul Polderman [polder at cs.vu.nl]):
--------------------------------------------------------------------
global.h:

#ifndef	GLOBAL
#define	GLOBAL	extern
#endif	GLOBAL

GLOBAL	int	i, j;
--------------------------------------------------------------------
global.c:

#define	GLOBAL
#include	"global.h"
--------------------------------------------------------------------
x.c:

#include	"global.h"

xyz()
{
	i = ...
}
--------------------------------------------------------------------
y.c:

#include	"global.h"

zzt()
{
	i = j ...
}
--------------------------------------------------------------------
cc x.c y.c global.c
--------------------------------------------------------------------
Regards.
-- 
South-Africa:                         |Maarten Litmaath @ Free U Amsterdam:
           revival of the Third Reich |maart at cs.vu.nl, mcvax!botter!ark!maart



More information about the Comp.lang.c mailing list