Path: utzoo!mnetor!uunet!mcvax!botter!ark!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.lang.c Subject: Re: global data in C programming... Message-ID: <1212@ark.cs.vu.nl> Date: 17 Mar 88 23:40:28 GMT References: <7506@ncoast.UUCP> Reply-To: maart@cs.vu.nl (Maarten Litmaath) Distribution: comp Organization: VU Informatica, Amsterdam Lines: 63 Keywords: global data, extern, C programming In article <7506@ncoast.UUCP> btb@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@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@cs.vu.nl, mcvax!botter!ark!maart