/* Fig. 5.12: fig05_12.c A scoping example */ #include void a( void ); /* function prototype */ void b( void ); /* function prototype */ void c( void ); /* function prototype */ int x = 1; /* global variable */ int main() { int x = 5; /* local variable to main */ printf("local x in outer scope of main is %d\n", x ); { /* start new scope */ int x = 7; printf( "local x in inner scope of main is %d\n", x ); } /* end new scope */ printf( "local x in outer scope of main is %d\n", x ); a(); /* a has automatic local x */ b(); /* b has static local x */ c(); /* c uses global x */ a(); /* a reinitializes automatic local x */ b(); /* static local x retains its previous value */ c(); /* global x also retains its value */ printf( "local x in main is %d\n", x ); return 0; } void a( void ) { int x = 25; /* initialized each time a is called */ printf( "\nlocal x in a is %d after entering a\n", x ); ++x; printf( "local x in a is %d before exiting a\n", x ); } void b( void ) { static int x = 50; /* static initialization only */ /* first time b is called */ printf( "\nlocal static x is %d on entering b\n", x ); ++x; printf( "local static x is %d on exiting b\n", x ); } void c( void ) { printf( "\nglobal x is %d on entering c\n", x ); x *= 10; printf( "global x is %d on exiting c\n", x ); } /************************************************************************** * (C) Copyright 2000 by Deitel & Associates, Inc. and Prentice Hall. * * All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/