Source of storage_linkage_main1.cpp


  1: //storage_linkage_main1.cpp
  2: //Goes with storage_linkage_aux1.cpp. Compile the two files
  3: //separately and link the object files to produce an executable.
  4: //Illustrates an (explicitly declared extern) integer variable n
  5: //in the body of the main function, which is defined in another 
  6: //file, and an (implicitly extern)  function DoItToN() declared
  7: //in the global namespace but also defined in that other file.

  9: #include <iostream>
 10: using namespace std;

 12: void DoItToN();

 14: int main()
 15: {
 16:     extern int n;  //This int variable is defined "elsewhere".
 17:     cout << "\nValue of n declared extern = " << n;
 18:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 20:     cout << endl;

 22:     DoItToN();
 23:     cout << "After DoItToN returns, n = " << n;
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 25: }

 27: /*
 28: Questions:
 29: 1. What effect will moving the first line of the body of main to
 30:    the global namepace of this file have on this program's output?
 31: 2. What effect will placing the keyword "extern" in front of the
 32:    prototype of the DoItToN() function have on this program's output?

 34: Answers:
 35: 1. None.
 36: 2. None. The function is "extern" by default.
 37: */