Source of storage_linkage_aux2.cpp


  1: //storage_linkage_aux2.cpp
  2: //Goes with storage_linkage_main2.cpp. Compile the two files
  3: //separately and link the object files to produce an executable.
  4: //These functions illustrate the difference between an
  5: //automatic local variable and a static local variable.

  7: #include <iostream>
  8: using namespace std;

 10: void DoItToA()
 11: {
 12:     int a = 5; //Here a is an "automatic" local variable.
 13:     ++a;
 14:     cout << "a = " << a << endl;
 15: }

 17: void DoItToB()
 18: {
 19:     static int b = 5;  //Here b is a "static" local variable.
 20:     ++b;
 21:     cout << "b = " << b << endl;
 22: }

 24: /*
 25: Question:
 26: What effect would placing the keyword "auto" in front of the
 27: definition of the local variable "a" in the body of DoItToA()
 28: have on this program's output?

 30: Answer:
 31: None. The variable "a" is "auto" by default.
 32: */