局部变量:
创建在栈区(向下生长),生命周期存在于当前作用域中。
创建时若未初始化,则为随机值。
全局变量:
创建在static区,生命周期一直存在。
创建时若未初始化,则为0。
静态局部变量:
切断了外部链接属性。创建在static区。
创建时若未初始化,则为0。全局变量和静态变量的存储是放在一块的,初始化了的全局变量和静态变量在一块区域,
未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。
全局静态变量:
切断了外部链接属性,创建在static区,生命周期一直存在。
创建时若未初始化,则为0。#include
#include
int fun1()
{
static int n = 0;
n++;
return n;
}
int main()
{
int ret = 0;
for (int i = 0; i
{
ret = fun1();
printf("%d\n", ret);
}
system("pause");
return 0;
}
输出1,2,3……10.#include
#include
static int n;
int fun1()
{
n = 0;
n++;
return n;
}
int main()
{
int ret = 0;
for (int i = 0; i
{
ret = fun1();
printf("%d\n", ret);
}
system("pause");
return 0;
}
输出10个1。
extern:
有外部链接属性,即可检测到外部文件。
static:
切断外部链接属性。
位于static区的变量:
整个生命周期均存在,如全局变量,静态变量。#include
#include
int b = 20;
int main()
{
static int a = 10;
int c = 5;
printf("%p\n", &a);
printf("%p\n", &b);
printf("%p\n", &c);
system("pause");
return 0;
}
由此可知,静态变量和全局变量的存储地址很近,在同一区域,即:均存储在static区。