Static Storage Class


 Static Storage class:
·       Variables stored in memory.
·       Default value of static variables is zero.
·       Scope of variables is local to block in which it is defined
·       Life of variables is till end of program. Its value initialized only one time and exists till complete execution.
·       auto and static both variables are local to block in which they defined, Difference only is that static variables don’t disappear when function is no longer alive. It remains in memory, so when control comes back to function , static values will be having same value as before.
·       As values of static variables kept in memory when variables are not active, So its wastage of memory. So we should use static variables only when you really need.

Example:


#include<stdio.h>
void test();    // Function Prototype

main( )
{
      test();
      test();
      test();
}

void test()
{
   static int i=1;   // Static variable
   i=i+1;
   printf("Function calling   %d  \n",i);
}



        In test function we declare a static variable. Its initial value is 1. So as many times we call this function, value of I increment by 1.

Comments

Popular posts from this blog

Disable or enable proxy for Internet explorer or Chrome

chm viewer unable to show contents

3 prisoners and 5 hats puzzle