Re: global variable declaration in header



In article <1180607788.103356.144860@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
nszabolcs <nszabolcs@xxxxxxxxx> wrote:

So is this style acceptable? (see example below)
[...]
// common.h:
int i;
void print();
void inc();

This is not good style. It defines (rather than merely declares) the
variable i in each file that includes common.h. Though, as the
standard and FAQ state, this works on many systems, it is better to
say what you mean, and merely declare i in the header file with
"extern int i", and define it in one of the .c files with "int i".

This matches what you do with functions, except that a function
declaration implicitly has then "extern" anyway. Some people prefer
to explicitly write "extern" even on function declarations, for
consistency.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
.



Relevant Pages

  • Re: Question about C code and use of "void"
    ... And I'm confused becasue void is, of course, a keyword. ... You are confusing #define syntax with typedef syntax. ... int INT; ... declares a typename called INT that is an alias for int. ...
    (microsoft.public.vc.language)
  • Re: Need some explanation
    ... >> required to document void main. ... >> extensions such as void main, ... implementation declares no prototype for this function. ... be defined with a return type of int and with no parameters: ...
    (comp.lang.c)
  • Re: Question about C code and use of "void"
    ... declares a variable called fnptr that is a pointer to a function that ... takes an int argument and returns void, ... pointer to a function that takes an int argument and returns void. ... A typedef can also be used to define a function type. ...
    (microsoft.public.vc.language)
  • Re: global variable declaration in header
    ... void print; ... It defines (rather than merely declares) the ... and merely declare i in the header file with ...
    (comp.lang.c)
  • [S390] New header file ipl.h
    ... Setup.h has been misused for ipl related stuff in the past. ... which has to do with ipl and reipl to a new header file named ... void *addr) ... +extern u32 ipl_flags; ...
    (Linux-Kernel)