区分静态变量

来源:互联网 发布:网页动态特效js代码 编辑:程序博客网 时间:2024/05/15 01:08
Code:
  1. ...   
  2. int global = 100;   
  3. static int one_file = 50;   
  4. int main()   
  5. {   
  6. ...   
  7. }   
  8.   
  9. void funct1( int n)   
  10. {   
  11.     static int count = 0;   
  12.     int llama = 0;   
  13.    ...   
  14. }   
  15.   
  16. void funct2( int q)   
  17. {   
  18. ...   
  19. }   

所有的静态持续变量在整个程序执行期间都存在。在funct1()中声明的变量count的作用域是局部,没有链接性,这意味着只能在funct1()函数中使用它,就像自动变量llama一样。但是和llama不同的是,及时在funct1()函数没有被执行时,count也留在内存中。global和one_file的作用域都为整个文件。由于one_file的链接性为内部,因此只能在包含上述代码的文件中使用它们,由于global的链接性为外部,因此可以在程序的其他文件中使用它。

在函数外部声明的变量加static  : 链接性为内部     在函数外部声明的变量,未加static,则系统默认加上extern:链接性为外部

只能使用常量表达是来初始化静态变量。可以使用字面值常量、const常量和enum常量以及sizeof操作符。

  

原创粉丝点击