vip% cat count.c /* count.c: counters--global, static, and local passed by reference. Program written by NR Wagner, Jan 10 97 */ #include #define TH(i) (i)/10%10 == 1 ? "th" :\ (i)%10 == 1 ? "st" :\ (i)%10 == 2 ? "nd" :\ (i)%10 == 3 ? "rd" : "th" void f0(void); void f1(void); void f2(void); void f3(int *counter3); int counter1 = 0; /* global counter */ void main(void) { int counter3 = 0; /* local counter, passed by ref */ int i; for (i = 1; i < 205; i++) { f0(); f1(); f2(); f3(&counter3); } } void f0(void) { int counter0 = 0; /* auto local counter */ /* DOESN'T WORK! */ counter0++; (void)fprintf(stdout, "%3d%s time f0 called\n", counter0, TH(counter0)); } void f1(void) { counter1++; /* global counter */ (void)fprintf(stdout, "%3d%s time f1 called\n", counter1, TH(counter1)); } void f2(void) { static int counter2 = 0; /* static local counter */ counter2++; (void)fprintf(stdout, "%3d%s time f2 called\n", counter2, TH(counter2)); } void f3(int *counter3) { (*counter3)++; /* global counter */ (void)fprintf(stdout, "%3d%s time f3 called\n", *counter3, TH(*counter3)); } vip% lint -m -u count.c vip% cc -o count count.c vip% count 1st time f0 called 1st time f1 called 1st time f2 called 1st time f3 called 1st time f0 called 2nd time f1 called 2nd time f2 called 2nd time f3 called 1st time f0 called 3rd time f1 called 3rd time f2 called 3rd time f3 called 1st time f0 called 4th time f1 called 4th time f2 called 4th time f3 called ... 11st time f1 called 11th time f1 called 12th time f1 called 13th time f1 called ... 21st time f1 called 21st time f1 called 22nd time f1 called 23rd time f1 called 24th time f1 called 101st time f1 called 101st time f1 called 102nd time f1 called 103rd time f1 called 104th time f1 called 111th time f1 called 121st time f1 called 121st time f1 called 122nd time f1 called