The Scope of C variables
One might ask can I do something like this:
int foo(int x);
int i, j;
printf("%d %d\n", i, j);
int foo(int x)
j = x * x;
in order to set
The answer is no.
Whenever a variable is declared inside a function (as
) or is a parameter (like
that name for some memory location exists only inside
doesn't really exist inside of the function
Because of this
in C, you don't have to keep track of
which variables are in use in which functions and be careful not to
It is perfectly valid to code one of our earlier examples as:
void foo(int x);
x = 42;
printf("After calling foo: x = %d\n", x);
void foo(int x)
x = 101;
printf("Inside foo: x = %d\n", x);
and have the value for
still be 42 after the call
This also explains why we could make two calls
int foo(int a, int b)
The variable (or parameter) we use is always the one of that name
that has been most recently declared.
On some occasions, it is reasonable to declare variables that are
outside of any of the functions in a program.
Such variables are called
and are usually
For example, we could write:
printf("The number of students is %d\n", num_stud);
num_stud = 12;
and we'd find that the program would indeed print out that the number
of students was 12.
Just because you can create global variables doesn't mean you should.
The basic rule of thumb is:
Don't use global variables.
Some of the reasons are
So when is it reasonable to use global variables?
There are basically two occasions:
Using local variables allows you to focus you attention on smaller
more manageable pieces of the program.
Globals force you to keep track of how they're being used throughout
the entire system.
If you're working with other people, you must coordinate who's
creating which global variables.
It is not valid to create two globals of the same name.
There are programming techniques
) that require
some separation of multiple instances of the same variable name.
Localizing the effects of algorithm steps reduces the cases
of unexpected behavior.
Localizing the effects of algorithm steps makes the program easier
for others to understand.
The basic rule thumb still applies though and these other cases are
not likely to come up in the programs you'll be doing for this class.
Sometimes one function indirectly calls another where we have
no control over the intermediate steps.
If the two functions need to communicate, then we generally need
(Such cases represent fairly advanced techniques relative to those
you'll be using in this class, but we'll see an example of this
in one of the case studies.)
If the entire program is built around some central data and if
access to that data is needed in nearly every function and particularly
if that is a large data structure, then it makes sense to make such
One last note: We have only scratched the surface of scoping in C.
For example, it is possible to declare variables that have scope
smaller than a function.
You can also position variables somewhere between local to a function
and global to the whole program by making them local to a