/* * static_local.c * * Looking at the 'static' keyword * in C as it applies to local variables * in funcions. * */ #include void f1(){ int a; a += 1; printf("in f1 : a = %i \n", a); } void f2(int init){ // init is a boolean 1=>init, 0=>'normal call' static int a; if (init){ a = 0; } else { a += 1; printf("in f2 : a = %i \n", a); } } // THIS IS BAD!!! // ... but the idea is that we're trying // to return a pointer to an int which is 3. // The problem is that once the function exits, // the 'value' variable is effectively gone. // So using it's (old) address outside the // the function would be a mistake. int* three(){ int value = 3; return &value; } int main(){ int i; for (i=0; i<5; i++){ f1(); } f2(1); // initialize a for (i=0; i<5; i++){ f2(0); // call it normally } return 0; }