Original: http://developer.51cto.com/art/201104/256820.htm
Static is a commonly used modifier in C + +, which is used to control the storage and visibility of variables. extern, "C" is a means to enable C + + to invoke C writing library files, if you want to use C to the compiler prompt to handle the function, then use extern "C" to illustrate.
I. Static keywords in the C language
In the C language, static can be used to modify local variables, global variables, and functions. In different situations, the effects of static vary.
(1) Modifying local variables
In general, the local variable is stored on the stack, and the life cycle of the local variable ends at the end of the statement block execution. However, if modified with static, the variable is stored in the static data area, and its life cycle continues until the end of the execution of the program. However, it is important to note that although the modification of a local variable with static, its life cycle and storage space have changed, but its scope has not changed, it is still a local variable, scope is limited to the statement block.
After modifying a local variable with static, the variable is initialized only at the first run and only once.
Such as:
- #include <stdio.h>
- void Fun ()
- {
- static int a=1; a++;
- printf ("%d\n", a);
- }
- int main (void)
- {
- Fun ();
- Fun ();
- return 0;
- }
Program execution results are: 2 3
The second time the fun () function is called, the value of a is 2, and no initialization assignment is made, and the self-increment operation is performed directly, so the result is 3.
For a static local variable, if it is not initialized, the system will automatically assign a value of 0 to the shaping variable, and for the character array, it will automatically be assigned a value of ' \ s '.
(2) Modifying global variables
For a global variable, it can be accessed either in the source file or in other sources in the same project (simply declare with extern).
Such as:
- Have file1.c
- int a=1;
- File2.c
- #include <stdio.h>
- extern int A;
- int main (void)
- {
- printf ("%d\", a);
- return 0;
- }
The execution result is 1
However, if the int a=1 is changed to the static int a=1 in file1.c;
Then the file2.c is inaccessible to the variable A. The reason is that the modification of the global variable with static changes the scope of its scope, from the original project visible to the source file is visible.
(3) Modifier function
With the static modifier, the same thing as modifying a global variable is changing the scope of the function.
Two. Static in C + +
Static in C + + has other functions, and if a function in C + + is decorated with static, it means that the function belongs to a class other than any particular object of this class, and if a variable in the class is static decorated, it means that the variable is owned by the class and all of its objects. They only have one copy in the storage space. Can be called through classes and objects.
Three. extern keyword
In the C language, the modifier extern is used before the declaration of a variable or function to illustrate that "this variable/function is defined elsewhere and is referenced here."
As can be seen in the above example, if you want to call the variable A in file1 in File2, you only need to declare with extern to invoke a, which is the role of extern. It is important to note that the location of the extern declaration is also related to its scope, and if it is declared in the main function, it can only be called in the main function and cannot be called in other functions. In fact, to invoke the functions and variables in other files, simply include the file with # # included in it, why use extern? This saves time by using extern to speed up the process of compiling the program.
In C + +, extern has another role to indicate the invocation specification of C or C + + functions. For example, invoking C library functions in C + + requires that the function to be referenced is declared in the C + + program with extern "C". This is for the linker, telling the linker to link with the C function specification when linking. The main reason is that C + + and C programs are compiled in the target code after the naming rules are different, use this to solve the problem of name matching.
Talking about static and extern key-words in C + +