Source of scope2.cpp


  1: //scope2.cpp
  2: //A nonsense program to test your understanding
  3: //of variable scope and lifetime.

  5: #include <iostream>
  6: using namespace std;

  8: char firstChar, secondChar;  //Global variables

 10: void P1()
 11: {
 12:     char firstChar;   //Local to P1

 14:     firstChar  = 'A'; //Are these
 15:     secondChar = 'B'; //local or global?
 16: }

 18: void P2()
 19: {
 20:     char secondChar;  //Local to P2

 22:     firstChar  = 'C'; //Are these
 23:     secondChar = 'D'; //local or global?
 24:     P1();
 25:     cout << firstChar << secondChar << endl;
 26: }

 28: int main()
 29: {
 30:     cout << endl;
 31:     firstChar = 'E';  //Are these
 32:     secondChar = 'F'; //local or global?
 33:     P2();
 34:     cout << firstChar << secondChar << endl;
 35: }