Suppose I have a pointer that I want to make globally available for multiple translation units. I want to define it in one place (foo.c), but allow multiple declarations for it in other translation units. The extern keyword tells the compiler that this is not a defining declaration for an object; the actual definition will appear elsewhere. It just makes the object name available to the linker. When the code is compiled and linked, all different translation units will refer to the same object under this name.
Suppose I also have a pointer that I want to make globally accessible for functions in one source file, but not have visible translation units. I can use the keyword "static" to indicate that the name of the object will not be exported to the layout.
Suppose I also have a pointer that I want to use only inside one function, but that has a pointer value between function calls. I can again use the keyword "static" to indicate that the object has a static degree; memory for it will be allocated when the program starts and will not be released until the program ends, so the value of the object will be saved between function calls.
#ifndef FOO_H #define FOO_H extern int *aGlobalPointer; extern void demoPointers(void); ... #endif #include "foo.h" int *aGlobalPointer; static int *aLocalPointer; void demoPointers(void) { static int *aReallyLocalPointer; }
John bode
source share