In C language, you can use the static keyword in the global scope: variable or function modified by the static keyword, only in this ". c file "is visible, while in other ". file C is invisible. For example, if the static function func is defined in the global scope of t1.c, the func function defined in t1.c cannot be used in t2.c, even if the func function is declared using the extern keyword in t2.c.
LabCodeAs follows:
1. the extern modifier is used by default.
/* File: t1.c * // * defines the function func. At this time, no modifier is used. ** therefore, the extern modifier */void func () {} is used by default (){}
/* File: t2.c * // * declares the func function, which is defined in t1.c **. Because the declared function is a function, the extern keyword is displayed, you can omit */extern void func (); int main () {func (); // call the func function return 0 ;}
Compilation result:
Compiled
2. After static modification, it is only visible in the. c file.
/* File: t1.c * // * defines the function func. In this case, use static modifier */static void func (){}
/* File: t2.c * // * declares the func function, which is defined in t1.c and modified with static */extern void func (); int main () {func (); // error. func is invisible in t2.c. Return 0 ;}
Compilation result:
3. How to declare static functions in the. c file that defines static functions?
From the above two routines, we can see that static can make the function (or variable) only in this ". c file is visible, while in other ". c file is invisible. However, we often need to "use this function before defining the code of a function". This requires us to "declare this function and then call it, then define the function ", so this involves how to declare the static function. Of course, it must be" defining the static function. declare the static function in file C ".
/* File: t1.c * // **************************** static declaration ******* * ********************** // static declaration, static defines static void func1 (); static void func1 () {}// func1 is a static function. the C file is invisible. The default definition is static void func2 (); void func2 () {} // func2, which is also a static function. in the c file, the statement is not visible. extern defines static void func3 (); extern void func3 () {}// func3 or static, although it is defined as extern/*************************** extern declaration **** ************************** // extern declaration, extern defines extern void func4 (); extern void func4 () {}// func4 is the extern // extern declaration. The default definition is extern void func5 (); void func5 () {} // func5 is the extern // extern declaration, static defines extern void func6 (); static void func6 () {} // func6 is static/***************************** default declaration ** **************************** // default declaration, void func7 () is defined by default; void func7 () {}// func7 is extern // is declared by default; extern defines void func8 (); extern void func8 () {} // func8 is extern // default declaration, static defines void func9 (); static void func9 () {} // func9 is static
/* File: t2.c * // * declares the func [1-9] function. These functions define */extern void func1 (); extern void func2 () in t1.c (); extern void func3 (); extern void func4 (); extern void func5 (); extern void func6 (); extern void func7 (); extern void func8 (); extern void func9 (); int main () {// All functions defined/declared by static are static ". c files are invisible: func1 (); // error. func1 is invisible to func2 () in t2.c; // error. func2 is invisible to func3 () in t2.c; // error. func3 is invisible to t2.c func4 (); func5 (); func6 (); // error. func9 cannot be seen in t2.c func7 (); func8 (); func9 (); // error. func9 return 0 is invisible in t2.c ;}
Compilation result:
Conclusion:
1. If a function declaration or definition is modified using static, the function is changed to static. c file, such as func1, func2, func3, func6, func9;
2. If you use static to modify the definition of a function that does not use static declaration, you will get a warning, such as func6 and func9.