fork download
  1. // Global, Auto, and Static Variable Declaration Example
  2. #include <stdio.h>
  3.  
  4. int globalvar = 2; // global variable initialized only once
  5. // and it's value is always held in memory
  6. //************************************************************
  7. // Function: printit
  8. //
  9. // Description: Justy increments and prints a set of local,
  10. // static, and global variables
  11. //
  12. // Parameters: none
  13. //
  14. // Returns: void
  15. //
  16. //*************************************************************
  17.  
  18. void printit ()
  19. {
  20. static int staticvar = 2; // static initialized only once
  21. // value is always held in memory
  22.  
  23. int autovar = 2; // local variable within printit
  24.  
  25. globalvar++;
  26. staticvar++;
  27. autovar++;
  28.  
  29. // Timeline 2
  30.  
  31. printf ("globalvar = %d \n", globalvar);
  32. printf ("staticvar = %d \n", staticvar);
  33. printf ("autovar = %d \n\n", autovar);
  34. } // printit
  35.  
  36. int main ()
  37. {
  38. int x; // local variable within main
  39.  
  40. // Time Line 1
  41.  
  42. // Call print function three times
  43. printit();
  44. printit();
  45. printit();
  46.  
  47. x = 5;
  48.  
  49. // Time Line 3
  50. return (0);
  51. } // main
  52.  
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
globalvar = 3 
staticvar = 3 
autovar   = 3 

globalvar = 4 
staticvar = 4 
autovar   = 3 

globalvar = 5 
staticvar = 5 
autovar   = 3