Mega Code Archive

 
Categories / C / Code Snippets
 

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 j = 0; for( j = 0; j < 5; j++ ) { test1(); test2(); } }