3.3.1.4 How Can I Use A Variable Defined In Another Source File?
Provided the variable defined in the other source file is not specified
static
or auto
, then adding a declaration (as opposed
to a definition) for that variable into the current file will allow you to access it. A
declaration consists of the keyword extern
in addition to the correct type
and the exact name of the variable specified in its definition, e.g.,
extern int systemStatus; // declare systemStatus for use here
This storage-class specifier is part of the C language and your favorite C textbook will provide more information on its usage.
The position of the declaration in the current file determines the scope
of the variable. That is, if you place the extern
declaration inside a
function, it will limit the scope of the variable to within that function. If you place it
outside of a function, it allows access to the variable in all functions for the remainder
of the current file.
Declarations are often placed in header files and then
#include
d into the C source code (see 5.14.1 Preprocessor Directives).