I have seen instances where global variables are seemingly doubly defined, but not? i.e.
/* myFile.h */
int myInt;
/* myFile.c */
#include <myFile.h>
int myInt;
I would've expected an error because that is doubly defining a variable, no? But apparently this works. I'd like to see the definition only once in the source file, and declarations (extern) done in header files as needed. I believe with functions, that the prototype is a declaration (extern) by default so using extern with a function does not do anything. But in this case it is global variables.
So I don't think the above is good practice but I'm curious how it even works.
Also, when you use a header file that provides variable and function definitions, i.e.:
/* myHeader.h */
int one = 1;
int someVar;
void someFunc()
{
....someVar = one;
}
Why would you do this? Does putting the function def in the header file force it to be inlined? Or is this just another bad thing to do but technically works?
Thanks.
/* myFile.h */
int myInt;
/* myFile.c */
#include <myFile.h>
int myInt;
I would've expected an error because that is doubly defining a variable, no? But apparently this works. I'd like to see the definition only once in the source file, and declarations (extern) done in header files as needed. I believe with functions, that the prototype is a declaration (extern) by default so using extern with a function does not do anything. But in this case it is global variables.
So I don't think the above is good practice but I'm curious how it even works.
Also, when you use a header file that provides variable and function definitions, i.e.:
/* myHeader.h */
int one = 1;
int someVar;
void someFunc()
{
....someVar = one;
}
Why would you do this? Does putting the function def in the header file force it to be inlined? Or is this just another bad thing to do but technically works?
Thanks.
