From this good answer , the best way to declare global variables in C is to declare the variable in 1 header using extern
, and define it in 1 source only. Then reference the header in other source files wanting to use the global variable.
So I did that, in my header global.h
:
extern Boolean transmitting;
Inside my main.c
, I defined it inside my main function:
int main() {
Boolean transmitting = FALSE;
...
}
However, I get a warning on the variable in the main "unused variable 'transmitting' [-Wunused-variable]"
which tells me this is only defined in the scope of the main, so other source files cant use it? Does this mean I need to define them outside main()
?
Previoulsy, I was not using the extern keyword to declare in my header and used the variable in my main like this:
int main() {
transmitting = FALSE;
...
}
Everything worked fine and other source files had access to the variable by including this header. However I want to use the correct method of declaring global variables. Where did I go wrong?