Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void useLocal( void ); //function prototype
  4. void useStaticLocal( void ); //function prototype
  5. void useGlobal( void ); //function prototype
  6.  
  7. int x = 1; //global variable
  8.  
  9. int main()
  10.  
  11. { // start scope
  12. int x = 5;
  13.  
  14. printf( "local x in outer scope of main is %d\n" , x );
  15.  
  16. { //start new scope
  17. int x = 7; //local variable to new scope
  18.  
  19. printf( "local x in inner scope of main is %d\n" , x );
  20. } //end new scope
  21. printf( " local x in outer scope is %d\n" , x );
  22.  
  23. useLocal(); //useLocal has automatic local x
  24. useStaticLocal(); //useStaticLocal has static local x
  25. useGlobal(); //useGlobal uses global x
  26. useLocal(); //useLocal reinitializes automatic local x
  27. useStaticLocal(); //static local x retains its prior value_type
  28. useGlobal(); //global x also retains its value
  29.  
  30. printf( "\nlocal x in main is %d\n" , x );
  31. } //end main
  32.  
  33. void useLocal( void )
  34. {
  35. int x = 25; // initialized each time useLocal is called
  36.  
  37. printf( "\nlocal x is useLocal is %d after entering useLocal\n" , x );
  38. ++ x;
  39. printf( "local x in useLocal is %d before exiting useLocal \n" , x );
  40.  
  41. } //end function useLocal
  42.  
  43. //useStaticLocal initializes static local variable x only the first time_base
  44. //the function is called; value of x is saved between calls to this
  45. //function
  46. void useStaticLocal ( void )
  47. {
  48. // initialized once before program startup
  49. static int x = 50;
  50.  
  51. printf( "\nlocal static x is %d on entering useStaticLocal\n" , x );
  52. ++ x;
  53. printf( "local static x is %d on exiting useStaticLocal\n" , x );
  54. } //end function useStaticLocal
  55.  
  56. //function useGlobal modifies global variable x during each call
  57. void useGlobal( void )
  58. {
  59. printf( "\nglobal x is %d on entering useGlobal\n" , x );
  60. x *= 10;
  61. printf( "global x is %d on exiting useGlobal\n" , x );
  62. }// end function useGlobal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement