I've recently begun to realize that the use of the "extern" keyword is highly encouraged. Thus I began to wonder if there is anything wrong with the current (extern-less) way I use header files:
main.c:
#include "main.h" #include "function.h" int main(void){ globalvariable = 0; testfunction(); return 0; } main.h:
#ifndef MAIN_H_ #define MAIN_H_ int globalvariable; #endif /* MAIN_H_ */ function.c:
#include "main.h" #include "function.h" void testfunction(){ globalvariable++; return; } function.h:
#ifndef FUNCTION_H_ #define FUNCTION_H_ void testfunction(void); #endif /* FUNCTION_H_ */ Thus every new source file that needs access to globalvariable simply needs to include main.h.
One obvious drawback to this method is arrays: you can't use {element0, element1, ...} formatting to assign values to an array once it has been declared.
By the way, when I give globalvariable an initial value of zero, am I defining it at that point? Or is memory allocated earlier?
Also, is there an official term for the method I'm using?
externkeyword is not highly encouraged; you should use it only when you absolutely must (which hopefully is never).