C语言 static的作用

来源:互联网 发布:mysql创建数据库 编辑:程序博客网 时间:2024/06/06 00:50
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条。(1)第一个作用:隐藏。当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性。为理解这句话,我举例来说明。我们要同时编译两个源文件,一个是a.c,另一个是main.c。下面是a.c的内容
<span style="background-color: rgb(255, 255, 255);">#include<cstdio>增加这条语句char a = ‘A‘; // global variablevoid msg() {     printf("Hello\n"); }</span>

你可能会问:为什么在a.c中定义的全局变量a和函数msg能在main.c中使用?前面说过,所有未加static前缀的全局变量和函数都具有全局可见性,其它的源文件也能访问。此例中,a是全局变量,msg是函数,并且都没有加static前缀,因此对于另外的源文件main.c是可见的。如果加了static,就会对其它源文件隐藏。例如在a和msg的定义前加上static,main.c就看不到它们了。利用这一特性可以在不同的文件中定义同名函数和同名变量,而不必担心命名冲突。Static可以用作函数和变量的前缀,对于函数来讲,static的作用仅限于隐藏,而对于变量,static还有下面两个作用。(2)static的第二个作用是保持变量内容的持久。存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化。共有两种变量存储在静态存储区:全局变量和static变量,只不过和全局变量比起来,static可以控制变量的可见范围,说到底static还是用来隐藏的。(3)static的第三个作用是默认初始化为0。其实全局变量也具备这一属性,因为全局变量也存储在静态数据区。在静态数据区,内存中所有的字节默认值都是0x00,某些时候这一特点可以减少程序员的工作量。最后对static的三条作用做一句话总结。首先static的最主要功能是隐藏,其次因为static变量存放在静态存储区,所以它具备持久性和默认值0。下面是main.c的内容除了头文件,需要声明函数:void msg();
<span style="background-color: rgb(255, 255, 255);">int main(void){        extern char a;    // extern variable must be declared before use     printf("%c ", a);     (void)msg();    return 0;}</span>
<span style="background-color: rgb(255, 255, 255);"></span><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 宋体; ">—————————————————————————————————</span><span style="font-size: 12pt; font-family: 宋体; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 'Arial Black'; ">本文原创自</span><span style="font-size: 12pt; font-family: 'Arial Black'; "><a target=_blank target="_blank" href="http://blog.csdn.net/jqh2002_blog" style="color: rgb(255, 153, 0); text-decoration: none; ">Slience</a><span style="font-family: 宋体; ">的</span>csdn<span style="font-family: 宋体; ">技术博客。</span></span><span style="font-size: 12pt; font-family: 'Arial Black'; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 'Arial Black'; ">本博客所有原创文章请以链接形式注明出处。</span><span style="font-size: 12pt; font-family: 'Arial Black'; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 'Arial Black'; ">欢迎关注本技术博客,本博客的文章会不定期更新。</span><span style="font-size: 12pt; font-family: 'Arial Black'; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><img src="http://img.blog.csdn.net/20140615132853109?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvanFoMjAwMl9ibG9n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" style="border: none; max-width: 100%; " /></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 黑体; ">大多数人想要改造这个世界,但却罕有人想改造自己。</span><span style="font-size: 12pt; font-family: 黑体; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 黑体; ">世上没有绝望的处境,只有对处境绝望的人。</span><span style="font-size: 12pt; font-family: 黑体; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 黑体; ">                                              ————By slience</span><span style="font-size: 12pt; font-family: 黑体; "></span></p><p class="p0" style="margin-top: 0pt; margin-bottom: 0pt; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px; "><span style="font-size: 12pt; font-family: 宋体; ">—————————————————————————————————</span></p>
0 0
原创粉丝点击