Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!emory!gatech!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!stl!sbil!wet!spitws111!harry From: harry@spitws111.uk.sun.com (Harry Protoolis) Newsgroups: comp.lang.c Subject: Re: Correct or Not or Old-fashioned or Bug Message-ID: <1991May21.081109.21224@sbil.co.uk> Date: 21 May 91 08:11:09 GMT References: Sender: news@sbil.co.uk Reply-To: harry.protoolis@sbil.co.uk Distribution: comp.lang.c Organization: Salomon Brothers International Limited Lines: 37 In article , zhoumi@nff.ncl.omron.co.jp (Zhou Mi) writes: |> |> When I am working in a project, I find that someone in my group write |> the following program. Though it seems work well, I still wonder if it |> is correct or better ?? |> |> -----file 1 (named pro_all.h) --------- |> int i; |> more stuff where this is included into many .c files and used ... This is not correct, you do not say what machine/OS you are using, but on many systems (most ?) this practice would generate a linker error (multiple definition of symbol i). Maybe a standard lawyer out there can tell me what ANSI says on this one. Anyway, if it is intended that i be a global variable it should be declared in the interface file (.h) as 'extern int i', and defined in one of the .c files as int i. This will generate one instance of the variable (in the implementation (.c) file containing the definition), which will be shared by the whole program (i.e a global variable). In general .h files should only contain declarations (i.e things like typedef and extern). 'int i' is a definition and therefore belongs in an implementation file (.c). I am suprised it works at all and it is certainly not portable, or good style. What machine/OS/Compiler are you using ???? Hope this helps, Harry -- 'When I give food to the poor they call me a saint. When I ask why the poor have no food they call me a communist.' - Dom Helder Camara