Static role (modified functions, local variables, global variables)

In the C language, the literal meaning of static can easily lead us astray. In fact, it has three functions.

( 1 ) Let's first introduce its first and most important one: hiding.

When we compile multiple files at the same time, all global variables and functions that are not prefixed with static have global visibility. To understand this sentence, let me illustrate with an example. We want to compile two source files at the same time, one is ac and the other is main.c.

Below is the content of ac

char  a  = 'A'// global variablevoid msg() {    printf("Hello\n"); }  



 Below is the content of main.c

int  main( void )
{    
    
extern char a;    // extern variable must be declared before use    printf("%c ", a);    (void)msg();return0;}  


    
 

The result of running the program is:

 

A Hello

You may ask: why the global variable a and function msg defined in ac can be used in main.c ? As mentioned earlier, all global variables and functions that are not prefixed with static have global visibility and can also be accessed by other source files. In this example, a is a global variable, msg is a function, and neither is prefixed with static , so it is visible to another source file main.c.

If static is added , it will be hidden from other source files. For example , add static before the definitions of a and msg , and main.c will not see them. Using this feature, you can define functions and variables with the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables. For functions, the role of static is limited to hiding, and for variables, static has the following two roles.

( 2 ) The second role of static is to keep the content of the variable persistent. The variables stored in the static data area will be initialized when the program starts running, and it is the only initialization. There are two types of variables stored in the static storage area: global variables and static variables, but compared with global variables, static can control the visible scope of variables. In the end, static is still used to hide. While this usage is uncommon, I'll give an example.

#include  < stdio.h > int  fun( void ){ static int  count  = 10 ;     //  in fact this assignment never executes return  count -- ; } int  count  = 1 ; int  main( void ) {         printf( " global\t\tlocal static\n " ); for (; count  <= 10 ++ count)         printf( " %d\t\t%d\n " , count, fun());    


    
   
    


 




    
 

    
    
return 0;}  

 The result of running the program is:

global          local static

1               10

2               9

3               8

4               7

5               6

6               5

7               4

8               3

9               2

10              1

( 3 ) The third role of static is to initialize to 0 by default . In fact, global variables also have this property, because global variables are also stored in the static data area. In the static data area, the default value of all bytes in memory is 0x00 , and this feature can reduce the workload of programmers in some cases. For example, to initialize a sparse matrix, we can set all elements to 0 one by one, and then assign several elements that are not 0 . If it is defined as static, the operation of setting 0 at the beginning is omitted . Another example is to use a character array as a string, but it is too troublesome to add '\0' at the end of the character array every time. If you define the string as static, you will save this trouble, because there is '\0' . Let's do a little experiment to verify it.

#include  < stdio.h > int a;int main(void){int i;staticchar str[10];    printf("integer: %d;  string: (begin)%s(end)", a, str);return0;}





    

    
 



    
 

The result of running the program is as follows

integer: 0; string: (begin)(end)

Finally , make a one-sentence summary of the three functions of static . First of all, the main function of static is to hide, and secondly, because static variables are stored in the static storage area, they have persistence and the default value of 0.

The above content comes from the hand of Mr. Write in the blog garden. The writing is quite clear and easy to understand, and the archive is convenient for review. Original address: http://www.cnblogs.com/dc10101/archive/2007/08/22/865556.html

The following is a question and answer question from the ZTE 2012 school recruitment test:

1. What is the difference between static global variables and ordinary global variables?

  全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量。

  全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式 这两者在存储方式上并无不同。

   这两者的区别在于非静态全局变量的作用域是整个源程序, 当一个源程序由多个源文件组成时,非静态的全局变量在各个源文件中都是有效的。 而静态全局变量则限制了其作用域, 即只在定义该变量的源文件内有效, 在同一源程序的其它源文件中不能使用它。由于静态全局变量的作用域局限于一个源文件内,只能为该源文件内的函数公用,因此可以避免在其它源文件中引起错 误。 

  static全局变量只初使化一次,防止在其他文件单元中被引用;   

2.  static局部变量和普通局部变量有什么区别 ?

   把局部变量改变为静态变量后是改变了它的存储方式即改变了它的生存期。把全局变量改变为静态变量后是改变了它的作用域,限制了它的使用范围。  

  static局部变量只被初始化一次,下一次依据上一次结果值;   

3.  static函数与普通函数有什么区别?

    static函数与普通函数作用域不同,仅在本文件。只在当前源文件中使用的函数应该说明为内部函数(static修饰的函数),内部函数应该在当前 源文件中说明和定义。对于可在当前源文件以外使用的函数,应该在一个头文件中说明,要使用这些函数的源文件要包含这个头文件.

  static函数在内存中只有一份,普通函数在每个被调用中维持一份拷贝

 

 

转自:http://www.cnblogs.com/stoneJin/archive/2011/09/21/2183313.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326861288&siteId=291194637