Re: global variable declaration in header




Richard Tobin wrote:
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".

thanks
i like minimalistic code, but it seems it's not quite ok to leave out
extern here

.



Relevant Pages

  • Re: need to compile this ( trying to find roots of a bisection)
    ... There is no such header file as conio.h. ... main returns an int, not void. ... If you are using some other language with vague ...
    (comp.lang.c)
  • 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: Function prototype vs implementation mismatch in C++
    ... there would be to inform the user of the API (header file). ... void display(const int); ... reason a slightly different name might be more appropriate. ...
    (alt.comp.lang.learn.c-cpp)
  • Passing CPtrList through a procedure
    ... declare the variable in the header file as a global variable. ... void Execute; ... level, int *comb, int cl); ... In the header file I declare a variable called myList as a CPtrList. ...
    (microsoft.public.vc.mfc)