Difference between putting variables in header vs putting variables in source
Say I declare a header file with a variable:
int count;
Then in the source file, I want to use count
. Do I have to declare it as:
extern int count
Or can I 开发者_如何学运维just use it in my source file? All assuming that I have #include "someheader.h"
. Or should I just declare it in the source file? What is the difference between putting count
in the header file vs the source file? Or does it not matter?
You only want one count
variable, right? Well this line:
int count;
Defines a count
variable for you. If you stick that in multiple files (by including it in a header), then you'll have multiple count
variables, one for each file, and you'll get errors because they'll all have the same name.
All the extern
keyword does is say that there is a count
variable defined in some other file, and we're just letting the compiler know about it so we can use it in this file. So the extern
declaration is what you want to put in your header to be included by your other files. Put the int count;
definition in one source file.
If you did put that into the header, then yes, you could just use it in the source file without any further declaration (after the point where the header has been #include
d, anyway).
#include "someheader.h"
effectively just copies the contents of someheader.h
in, as if it had all been directly written in the including file at that point.
However, this is not the way you're supposed to use headers. int count;
is a tentative definition - you are supposed to only put declarations in header files. So someheader.h
should have:
extern int count;
(which is just a declaration), and exactly one source file in your application should define count
:
int count = 0;
The others can just #include "someheader.h"
and use count
.
The difference if that if you put
int count;
in the header, you'll get a redefinition error if the header's included in more than one source file Putting that line in the source file only will result in a var available only in that source file (code in other source files will not be aware of that var and you'll be able to declare another var with that name).
In the header, you have to put
extern int count;
and in the sounrce file
int count;
This will result in declaring a global var, available to all source files that include your header. And no redefinition errors.
The file that actually gets compiled is what the preprocessor spits out, not the source.c file.
So, if you put int count;
in a header file, every source file which #include
s the header will get it's own copy of count
.
In this case you'll have source which looks like this:
int count;
...
extern int count;
If you're using gcc, try gcc -E file.c
. This will instruct it to only run the preprocessor so you can see what's actually being fed to the compiler.
As @Neil suggested, you will want to declare int count;
in a C file. If you want another C file to be able to reference this variable, then you put an extern int count;
declaration in the other file (or in a header file that the other includes).
Incidentally, one of my favorite C bugs is when you declare a global variable like this: int count;
, then in another file you declare another global variable with the same name, but a different type float count;
. In the first file, you say count = 1
, and all the sudden the count
in the second file becomes -0.0
. Moral of the story? Avoid global variables, and if you must use them, make them static
.
List item
精彩评论