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
countvariable, right? Well this line:Defines a
countvariable for you. If you stick that in multiple files (by including it in a header), then you’ll have multiplecountvariables, one for each file, and you’ll get errors because they’ll all have the same name.All the
externkeyword does is say that there is acountvariable defined in some other file, and we’re just letting the compiler know about it so we can use it in this file. So theexterndeclaration is what you want to put in your header to be included by your other files. Put theint count;definition in one source file.