10.1 局部变量
1. 静态变量
静态变量永久拥有其占用的存储空间
#include <stdio.h>
void test1();
int main()
{
test1();
test1();
test1();
}
void test1()
{
static int i=0;
int j=0;
i++;
j++;
printf("静态的 i = %d,局部的 j = %d\n",i,j);
}
// 输出
静态的 i = 1,局部的 j = 1
静态的 i = 2,局部的 j = 1
静态的 i = 3,局部的 j = 1
10.2 外部变量(全局变量)
全局变量位于所有函数体之外(包括 main 函数),全局变量的声明周期是从文件开始,到文件结尾。它拥有和 static 一样的永久存储空间。
#include <stdio.h>
void test1();
// c 就是一个全局变量
int c=0;
int main()
{
test1();
test1();
test1();
test1();
}
void test1()
{
c++;
printf("%d\n",c);
}