Static keywords & lt; 1 & gt;

Source: Internet
Author: User

Static can be said to have been accompanied by our programming career. There are too many things about his purposes and benefits. It is very easy for beginners to get dizzy and dizzy by static, even if you understand something, remember something conceptual. Today I use code to give you a deeper understanding of static. today, we mainly analyze static and OK in C language, starting from the variable 1. if a static local variable defines a local variable, its lifecycle must be the end of the current function. However, after being modified by static, its declaration cycle will be extended to the end of the entire program, and stored in the global zone, has it become a global variable? # Include <stdio. h> int func (int r) {static int n = 10; return r * n;} int main (int argc, char * argv []) {printf ("% d \ n", func (10); printf ("n = % d \ n", n); return 0 ;}found: error C2065: 'N': undeclared identifier says n is undefined, which means that he has not programmed global variables. So: static local variables increase his lifecycle to the end of the entire program, but his scope has not changed, it is still returned by the function where it is located. 2. static global variables assume that we have three files: test. h test. c main. ctest. h: Put some declaration of function or global variable test. c: Set test. add the h header file and test. the function or variable declared in h defines main. c: Use test. the variable or function in h does not need static modification Code as follows: test. h: # define _ TEST_H # define _ TEST_Hextern int g_global; # endiftest. c: # include "test. h "int g_global = 100; main. c: # include <stdio. h> # include "test. h "int main () {printf (" % d \ n ", g_global);} gcc main. c test. c finds that the program has no problems. The output result is 100. Of course, we are talking about static. Add static to try it. h: # defi Ne _ TEST_H # define _ TEST_Hextern static int g_global; # endiftest. c: # include "test. h "static int g_global = 100; main. c: # include <stdio. h> # include "test. h "int main () {printf (" % d \ n ", g_global);} found to be accessible, however, if the result is 0, doesn't the static global variable be accessible only in the file that defines it, but why are we still accessing it in main? But in another way, write: test. h: # define _ TEST_H # define _ TEST_Hextern int g_global; void print (); # endiftest. c: # include "test. h "int g_global = 100; static int g_static = 123; Void print () {printf ("g_static = % d \ n", g_static);} main. c: # include <stdio. h> # include "test. h "int main () {printf (" % d \ n ", g_static); print () ;}the error is returned, indicating that the sentence is not correct. Conclusion: the life cycle of the static global variable is at the end of the entire project, but the access scope of this global variable is limited to the file that defines it. If the static global variable is declared in the header file, the macro is replaced in the main function, and the declared static variable is not initialized, therefore, the output value is 0. In the second method, no statement is declared after macro replacement, so an error occurs. 3. if a static function does not include a static declaration function, it can be called if it is directly added to other files. However, if static is added, then the scope of this static function is limited to the file that defines it. Similar to static global variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.