cottrell@nbs-vms.ARPA (01/04/85)
/* 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; 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; not every var need be either initialized or even declared unless the compiler (bdsc for one?) requires it. there is nothing wrong with both a declaration (extern) and a definition (no extern) appearing in the same file, and it seems clear to me. this is the centralized approach. 2) other files just declare a var where they need to, probably only if they desire initialization. 3) i only said two "where's the beef?" "dont listen to what i say, hear what i mean" */
fnf@unisoft.UUCP (Fred Fish) (01/06/85)
>> 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.
jsdy@SEISMO.ARPA (01/07/85)
> 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; > > 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; > ... > 2) other files just declare a var where they need to, probably only if > they desire initialization. This is almost exactly what I'd rather n o t be done. (Notice, by the way, that (2) will always conflict with the identical declaration in (1) if the "extern" storage class is not considered default.) The C Reference Manual states that a missing storage class defaults to "extern" outside a function (sec. 8.1, K&R p. 193, last para). I'd rather leave it at that. However, many C compilers ignore this. To be compatible with them, I'd prefer to use an explicit "extern" in all header (include) files. Definition and initialisation of a variable should occur in that module in which that variable is most used. (This could be the main module, if the data is (*shudder*) used everywhere.) Side benefits: we don't have to keep travelling to data.c/global.c to see what something r e a l l y is; we may find that some extern's can become static's; localisation of data becomes easier; and probably some others. Joe Yao (UUCP!seismo!hadron!jsdy / hadron!jsdy@seismo.ARPA)
cottrell@nbs-vms.ARPA (01/08/85)
/* okay fred fish, you've got the best way to do it. thats the beauty of the scientific method, the adoption of a better way of doing things. */
GEACC022%TIMEVX%CITHEX@lbl.arpa (01/11/85)
Received: from timevx by cithex with DECNET ; Thu, 10 Jan 85 15:53:05 PST Date: Thu, 10 Jan 85 15:53:43 PST From: geacc022 (ansok, gary e.) @ timevx Message-Id: <850110155338.00b@timevx> Subject: global declarations To: info-c @ brl-tgr.arpa I had always thought that the "right way" to declare global variables was: 1) "extern" declaration in the .h file 2) definition (and initialization) in one .c file Remember, it is legal to have both an "extern" declaration and a definition in the same file (or at least it should be -- K&R, p. 77). I haven't seen the standard, so I don't know what it says about this. Gary Ansok GEACC022%TIMEVX%CITHEX @ LBL-G.ARPA GEA @ CALTECH.BITNET ...{ucbvax,ihnp4}!cithep!timevx#geacc022 "All the world loves a straight man."