autoscp.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // autoscp.cpp -- illustrating scope of automatic variables
  2. #include <iostream>
  3. void oil(int x);
  4. int main()
  5. {
  6. using namespace std;
  7. int texas = 31;
  8. int year = 2011;
  9. cout << "In main(), texas = " << texas << ", &texas = ";
  10. cout << &texas << endl;
  11. cout << "In main(), year = " << year << ", &year = ";
  12. cout << &year << endl;
  13. oil(texas);
  14. cout << "In main(), texas = " << texas << ", &texas = ";
  15. cout << &texas << endl;
  16. cout << "In main(), year = " << year << ", &year = ";
  17. cout << &year << endl;
  18. // cin.get();
  19. return 0;
  20. }
  21. void oil(int x)
  22. {
  23. using namespace std;
  24. int texas = 5;
  25. cout << "In oil(), texas = " << texas << ", &texas = ";
  26. cout << &texas << endl;
  27. cout << "In oil(), x = " << x << ", &x = ";
  28. cout << &x << endl;
  29. { // start a block
  30. int texas = 113;
  31. cout << "In block, texas = " << texas;
  32. cout << ", &texas = " << &texas << endl;
  33. cout << "In block, x = " << x << ", &x = ";
  34. cout << &x << endl;
  35. } // end a block
  36. cout << "Post-block texas = " << texas;
  37. cout << ", &texas = " << &texas << endl;
  38. }