C - Data Type External Variables


You can reference global variables as external to the current source file using the extern keyword.

Suppose you have global variables defined in another file by the statements:

int number = 0;
double d = 2.54;

In a source file in which you want to access these, you can specify that these variable names are external by using these statements:

extern int number;
extern double d;

These statements don't create these variables.

They identify to the compiler that these names are defined else where.

These names should apply to the rest of this source file.

The variables you specify as extern must be declared and defined somewhere else in the program, usually in another source file.

To make these external variables accessible to all functions within the current file, declare them as external at the very beginning of the file, prior to any of the function definitions.

With programs consisting of several files, you could place all initialized global variables at the beginning of one file and all the extern statements in a header file.

The extern statements can be included into .c file.

Related Topics