Correct or Not or Old-fashioned or Bug

Harry Protoolis harry at spitws111.uk.sun.com
Tue May 21 18:11:09 AEST 1991


In article <ZHOUMI.91May20182038 at marlboro.nff.ncl.omron.co.jp>, zhoumi at 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



More information about the Comp.lang.c mailing list