五种存储变量补充~作用域和存储时期

1.作用域指的是:在某个区域中可以   访问   该变量

2.存储时期指的是:表示该变量在程序执行期间一直存在,能不能 访问 就是另一回事了

  • 主要需要留意的是:空链接的静态变量
  • 具有代码块作用域或者函数作用域但是却是静态存储时期的

举个例子:

#include<stdio.h>

void test(void)
{
  int fade = 1;
  
static int a=1;   printf("fade = %d and stay = %d ", fade++,a++); } int main() {
  int count;
  for(count = 1;count <= 3;count++)
  {  
    printf("here comes iteration %d: ",count);
    test()
  }
}

输出结果为:
Here comes iteration 1:
fade = 1 and stay = 1
Here comes iteration 2:
fade = 1 and stay = 2
Here comes iteration 3
fade = 1 and stay = 3
  • static存储类说明符声明具有文件作用域的变量时,表示的是内部链接还是外部链接(有static修饰为内部链接)。
  • static声明具有代码块作用域变量时,给变量提供了静态存储时期。

注意:具有文件作用域的变量自动具有静态存储时期。

3.C语言种有五个作为存储类说明符的关键字

static,auto,register,extern,typedef(与内存存储无关,有无语法原因归于此类)

原文地址:https://www.cnblogs.com/sjxbg/p/5488791.html