Static Variable Examples in C

 /*  
  * static_c.c  
  *   
  * Copyright 2014 Afiz   
  *   
  *   
  */  
 #include <stdio.h>  
 void statfunc();   
 int main(int argc, char **argv)  
 {  
      int i=0;   
      for(i=0;i<10;i++)  
           statfunc();   
      return 0;  
 }  
 void statfunc()  
 {  
      static int stat_var=0;   
      int local_var =0;   
      printf("static valuce = %d\t Normal Variable =%d\n",stat_var, local_var);  
      stat_var++;   
      local_var++;   
 }  

/**

static variable is local to particular function. However, it is only initialised once (on the first call to function).
Also the value of the variable on leaving the function remains intact. On the next call to the function the the static variable has the same value as on leaving.
**/

Post a Comment

0 Comments