Home Segments Index Top Previous Next

703: Mainline

To declare a global variable, you precede its type declaration with the symbol extern.

For example, suppose that the global variable named mode is used to decide which of various alternative computational paths are to be taken in functions defined in multiple files. Such a variable might, for example, control a speed-versus-accuracy tradeoff.

To be accessible in multiple files, mode must be defined in one file and declared in the others:

The mode variable is defined as a global variable in this file 
  | 
  |             Same global mode variable is declared in these files 
  |                       |                       | 
  v                       v                       v 
*------------------*    *------------------*    *------------------*     
| int mode;        |    | extern int mode; |    | extern int mode; |     
|                  |    |                  |    |                  |     
|                  |    |                  |    |                  |     
|                  |    |                  |    |                  |     
*------------------*    *------------------*    *------------------*