Global variable exist throughout the life of the program · known to all progran AVOID GLOBAL VARIABLES WHENEVER POSSIBLE (for information hidden) PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Global Variable • exist throughout the life of the program • known to all program • AVOID GLOBAL VARIABLES WHENEVER POSSIBLE (for information hidden)
unsigned long charcnts [UCHAR MAX 1];/*0. UCHAR MAX * int main o i void print counters(void)i int ci while(c getchar),C= EOF charcnts [c]++i print counters ()i re七urn0 void print counters(void) t int ii fox(立=0;主< UCHAR MAX;i++) if (charcnts [i])/* write count only when nonzero * printf("\\03o " i)i isprint(i)? printf((c)",i): printf printf(":lu\n", charcnts [i]); PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone Kone
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu unsigned long charcnts[UCHAR_MAX + 1]; /* 0..UCHAR_MAX */ int main() { void print_counters(void); int c; while(c = getchar(), c != EOF) charcnts[c]++; print_counters(); return 0; } void print_counters(void) { int i; for (i = 0; i < UCHAR_MAX; i++) if (charcnts[i]) /* write count only when nonzero */ { printf("\\%03o ", i); isprint(i) ? printf("(%c)", i) : printf(" "); printf(": %lu\n", charcnts[i]); } }
Storage Classes A variables storage class provides information about its visibility, lifetime, and location eauto register estatic extern PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Storage Classes A variable’s storage class provides information about its visibility, lifetime, and location. •auto •register •static •extern
Storage Class- Auto it is the defaultstorage class it derives from automatic creation and removal on block entry and exit it is used to make local variable explicit it is stored in memory auto intx, y, intx, y; PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Storage Class - Auto • it is the default storage class • it derives from automatic creation and removal on block entry and exit • it is used to make local variable explicit • it is stored in memory { { auto int x, y; int x, y; . . } }
Storage Class it is the defaultstorage class it derives from automatic creation and removal on block entry and exit it is used to make local variable explicit it is stored in memory auto intx, y, intx, y; PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu
PROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Storage Class • it is the default storage class • it derives from automatic creation and removal on block entry and exit • it is used to make local variable explicit • it is stored in memory { { auto int x, y; int x, y; . . } }