Variables in Functions: Static versus automatic variables - C Function

C examples for Function:static

Description

Variables in Functions: Static versus automatic variables

Demo Code

#include <stdio.h>

void test1(void);
void test2(void);

int main(void){
  for(int i = 0 ; i < 5 ; ++i)  {
    test1();/*w w  w . j  a v  a  2  s.co m*/
    test2();
  }
  return 0;
}

void test1(void){
  int count = 0;
  printf("test1   count = %d\n", ++count );
}

void test2(void){
  static int count = 0;
  printf("test2   count = %d\n", ++count );
}

Related Tutorials