The Question: Is there anywhere in a program that a name declared as an extern cannot be accessed?
Actually, we'll just discuss this one.
If you review the definition of extern, then it should be obvious that accessing an extern anywhere after its declaration is perfectly valid -- we've done it in every program we've written thus far, each time we use the directive:
#include <iostream>This inserts (among other things) declarations of the names cin and cout into a program.
To see this, comment out the line with the #include
directive and recompile.
What error is produced ?
This insertion occurs outside of any pair of braces, and so
cin and cout are extern names.
If you remember, we always include the iostream header file prior to the main function. According to the scoping rule, the names declared in iostream can be accessed at any point between the #include directive and the end of the file. This means that not only can main access cin and cout, but any function following the #include directive can access them as well.
To see the importance of this, uncomment the #include directive, and then use cut-and-paste to move the #include directive so that it is between the main function and function Change().
Observation: Translate your source program. What is displayed?
Conclusions: Explain what happened in terms of
the definition of extern.
Forward to the Next Experiment