C language memo --static

  For me this keyword has not been clear today, specifically to spend some time to understand this keyword. When a function or variable declaration, after adding static data types before have the following effects

  A, a function definition:

    1, the function of link properties will be modified, changed from internal extrenal

    2, the function of the storage type and scope unaffected

  Second, when used in variable declarations:

    1, the type of the variable from memory automatically become static

    2, link attributes and scoping unaffected

    3, the life cycle will be extended to the end of the process

  Third, the most important point:

    Whether it is a function or variable, but a branded static then they only have access to the file is declared in!

 

In particular, I give 2.3 for chestnuts:  

 1 #include <stdio.h>
 2 
 3 int fun(void);
 4 
 5 int main(void)
 6 {
 7     ……
 8     fun();
 9     ……
10     return 0;
11 }
12 
13 int fun()
14 {
15   static int a;
16    ……    
17 }

 

  Here, if no int a fun in that a static qualifier will be recovered when the end fun, from the scope of the line 17 the function start to finish. But now is different, a variable declaration into a cycle of the entire program from the original function, the program starts running there will be a variable a, and a variable to the end of the program will be recycled.

  But, but, but its role is still scope becomes the same!

Remember, whether it is a function or variable, static will not change the scope, but will change the link field !

 

Guess you like

Origin www.cnblogs.com/daker-code/p/11619631.html