Static versus automatic variables : Static « Data Type « C / ANSI-C






Static versus automatic variables

#include <stdio.h>

/* Function test1 with an automatic variable */
void test1(void)
{
   int count = 0;
   printf("\ntest1   count = %d ", ++count );
}

/* Function test2 with a static variable */
void test2(void)
{
   static int count = 0;
   printf("\ntest2   count = %d ", ++count );
}

int main()
{
   int i = 0;

   for( i = 0; i < 5; i++ )
   {
     test1();
     test2();
   }
}



           
       








Related examples in the same category

1.Demonstrate the use for permanent and temporary storage
2.Define static variable inside function
3.Find out the address of a static variable in a functionFind out the address of a static variable in a function
4.Define and use static variable