The four storage categories of Storage Refiguration Auto, Register, Extern, and Static are provided in the C language. There are two storage periods for the four storage categories: automatic storage period and static storage period. The auto and register correspond to the automatic storage period. Variables with automatic storage period are established in the block of declaring the variable, and it exits when the block activity is exited.
Keywords extern and static are used to illustrate variables and functions with static memoryiod. The local variables declared with Static can only be identified by the function of the variable, but different from the automatic variable is that the STATIC variable still retains its value after the function is called. When the next function is called, you can access the last modified value. The statement of Static variables is as follows:
Static int Si = 1;
Some specific functions can be implemented due to STATIC's above features. The other two uses will be described below.
1. The number of statistical functions called
Declare a partial variable of the function and set it to the Static type, as a counter so that the function can be counted each time the function is called. This is the best way to quote the number of statistical functions, because this variable is closely related to the function, and the function may be called in multiple different places, so it is difficult to count from the caller's perspective. The test code is as follows:
/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------
s_fun.c
To Count The Number of a Function's Being Called, Use static var.
* /
#include
INT FUN_1 (INT);
int main ()
{
INT I;
For (i = 1; i <= 5; i )
FUN_1 (i);
Return 0;
}
INT FUN_1 (INT X)
{
Static count = 0;
COUNT ;
Printf ("I Have Been Called% D Times./N", count);
Return 2 * x;
}
The output is:
I Have Been Called 1 Times.
I Have Been Called 2 Times.
I Have Been Called 3 Times.
I Have Been Called 4 Times.
I Have Been Called 5 Times.
2. Reduce the overhead of local array build and assignment
The establishment and assignment of the variable is a storage type that requires a certain processor overhead, particularly an array or the like. In some functions containing more variables and is called, some arrays can be declared as a Static type to reduce the overhead of establishing or initializing these variables. The sample code is as follows:
/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------
Array_1.c
To test static arch
* /
#include
#include
#include
#define array_size 10000
#define call_times 30000
INT FUN_1 ();
INT FUN_2 ();
int main ()
{
INT I;
CHAR STRING2 [10], * String3;
Time_t t;
Time (& T); string3 = CTIME (& T);
Printf ("TIME 1:% S", String3);
For (i = 1; i <= call_times; i )
{
FUN_1 ();
}
Time (& T);
String3 = CTIME (& T);
Printf ("TIME 2:% S", String3);
For (i = 1; i <= call_times; i )
{
FUN_2 ();
}
Time (& T);
String3 = CTIME (& T);
Printf ("TIME 3:% S", String3);
Return 0;
}
INT fun_1 ()
{
Int a [array_size], b [array_size];
INT I, T;
For (i = 0; i { a [i] = i; B [i] = array_size - i; } For (i = 0; i { T = a [i]; a [i] = b [i]; B [I] = T; } Return 0; } INT FUN_2 () { Static Int A [Array_Size], B [Array_Size]; INT I, T; For (i = 0; i { a [i] = i; B [i] = array_size - i; } For (i = 0; i { T = a [i]; a [i] = b [i]; B [I] = T; } Return 0; } After several running, the typical results are as follows: lab environment: PC: Intel C 2.4D, 256M DDR333, 80G 7200RPM OS: WIN XP Professional, SP2 Compiler: TC 2.0 REFERENCE: 1. H. M. Deitel, P. J. Deitel, CHOW TO Program. 1994, Prentice Hall. 2002, China Mechine Press.