最近打算重新过一过C语言,重新在理解一下;
这节我们说说关键字static,最常见的用法那就是修饰变量,变量又分为局部和全局变量,但它们都存在内存的静态区;
1.静态全局变量,作用域 仅限于变量被定义的文件中,其他文件即使用 extern 声明也没法使用他。准确地说作用域是从定义之处开始,到文件结尾处结束,在定义之处前面的那些代码行也不能使用它,想要使用就得在前面再加 extern xxx。如果不想在前面加extern,直接在文件顶端定定义就可以了。
2.静态局部变量,在函数体里面定义的,就只能在这个函数里用了,同一个文档中的其他
函数也用不了。由于被 static 修饰的变量总是存在内存的静态区,所以即使这个函数运行结束,这个静态变量的值还是不会被销毁,函数下次使用时仍然能用到这个值。
来看一看下面这个程序,分别在函数内定义静态局部变量:
#include "stdafx.h"
//static int j;
void function1(void)
{
static int i = 0;
printf("the i %d\n", i);
i++;
}
void function2(void)
{
static int j = 0;
printf("the j %d\n", j);
j++;
}
int main()
{
printf("hello world\n");
static int k;
for (k = 0; k < 10; k++)
{
function1();
function2();
}
//printf("the j %d\n", j);
return 0;
}
可以先思考一下,程序结束时i和j的值,可以肯定的是这里i和j的值肯定是相同的;
C:\Users\curtis\Desktop\VS\static\Debug>static.exe
hello world
the i 0
the j 0
the i 1
the j 1
the i 2
the j 2
the i 3
the j 3
the i 4
the j 4
the i 5
the j 5
the i 6
the j 6
the i 7
the j 7
the i 8
the j 8
the i 9
the j 9
那如果换成下面这段呢?在文件开始位置定义全局变量
#include "stdafx.h"
static int j;
void function1(void)
{
static int i = 0;
printf("the i %d\n", i);
i++;
}
void function2(void)
{
//static int j = 0;
//printf("the j %d\n", j);
j = 0;
j++;
}
int main()
{
printf("hello world\n");
static int k;
for (k = 0; k < 10; k++)
{
function1();
function2();
}
printf("the j %d\n", j);
return 0;
}
C:\Users\curtis\Desktop\VS\static\Debug>static.exe
hello world
the i 0
the i 1
the i 2
the i 3
the i 4
the i 5
the i 6
the i 7
the i 8
the i 9
the j 1
他的第二个作用就是修饰函数,函数前加 static 使得函数成为静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件(所以又称内部函数)。使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名。
关键字 static 有着不寻常的历史。起初,在 C 中引入关键字 static 是为了表示退出一个
块后仍然存在的局部变量。随后,static 在 C 中有了第二种含义:用来表示不能被其它文件访问的全局变量和函数。为了避免引入新的关键字,所以仍使用 static 关键字来表示这第二种含义。
比如在写linux内核驱动时经常会用到这个关键字;
#include <linux/delay.h>
#include <linux/version.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Access non-exported symbols");
MODULE_AUTHOR("curtis li");
/**********************************************
* this function for kallsyms_lookup_name test*
*********************************************/
static int __init lkm_init(void)
{
char *sym_name = "resume_file";
unsigned long sym_addr = kallsyms_lookup_name(sym_name);
char filename[256];
strncpy(filename, (char *)sym_addr, 255);
printk(KERN_INFO "[%s] %s (0x%lx): %s\n", __this_module.name, sym_name, sym_addr, filename);
return 0;
}
static void __exit lkm_exit(void)
{
}
module_init(lkm_init);
module_exit(lkm_exit);