Mega Code Archive

 
Categories / C / Code Snippets
 

Define and use Global variables

#include <stdio.h> int count = 0; /* Declare a global variable */ /* Function prototypes */ void t1(void); void t2(void); void main() { int count = 0; /* This hides the global count */ for( ; count < 5; count++) { t1(); t2(); } } /* Function t1 using the global variable */ void t1(void) { printf("\nt1 count = %d ", ++count); } /* Function t2 using a static variable variable */ void t2(void) { static int count; /* This hides the global count */ printf("\nt2 count = %d ", ++count); }