Scope in C


Citation

Paper

Bibliographic Information

Abstract

The scope of a variable or function is that part of the program in which it can be referenced. A global variable is one that can be used anywhere in the program; a local variable is one that can only be used within a particular function.

Figuring out the scope of a variable in C is more complicated than in most other languages. For one thing, C programs are rarely compiled as a unit; most have several files of source code, each file being compiled separately and the resulting objects linked together to form an executable module. Variables can be made global to all functions in all files, or to the functions in one file only; local variables can be made local to a function or to a block, and the compiler can be instructed to reserve storage for them which will remain allocated after the function or block exits. Most languages do not have these features.

Copyright Notice

©1986 by Matt Bishop. The definitive version was published in The C Journal 2(1), Spring 1986.