C高级之Static用法

Static用法

示例1

#include <stdio.h>

#include <unistd.h>

 

int fun(void);

 

int main(int argc, const char *argv[])

{

       while(1)

       {

              fun();

              sleep(1);

       }

 

       return0;

}

 

int fun(void)

{

       intnum = 1;

 

       num++;

       printf("num= %d\n",num);

 

       return0;

}

我们看一下程序输出:

num = 2

num = 2

num = 2

如果我们把fun函数改为如下:

 

int fun(void)

{

       staticint num = 1;

 

       num++;

       printf("num= %d\n",num);

 

       return0;

}

我们看一下程序输出:

 

num = 2

num = 3

num = 4

为什么num经过static修饰后,num的值会不断增加呢?

这就是我们要说的static第一个用法:static修饰局部变量

static 修饰局部变量时,该变量称为静态局部变量,静态局部变量的存储空间在静态存储区,与函数的堆栈区不在同一个区域,因此函数运行完毕后静态局部变量不会被释放。静态局部变量只赋一次初值。下次使用静态局部变量的时候直接使用上次保存下来的值而不会重新赋值。因此,当num经过static修饰后,num只被赋值一次,而且fun函数被main调用完后num不会被释放,main下次再调用fun后,num的初值为上次fun退出后的值。

 

 

示例2

有一个工程有两个c文件,一个是hello.c文件,一个是main.c文件。

在hello.c文件中

#include <stdio.h>

 

int hello(void)

{

       printf("helloworld!\n");

 

       return0;

}

 

int hello1(void)

{

       hello();

 

       return0;

}

在main.c 文件中

#include <stdio.h>

int hello(void);

int hello1(void);

 

int main(int argc, const char *argv[])

{

       hello1();

       hello();

 

       return0;

}

我们看一下程序输出:

 

hello world!

hello world!

 

如果我们在在hello.c 文件中,在hello函数前加static

staic int hello(void)

{

       printf("helloworld!\n");

 

       return0;

}

 

我们看一下编译情况

在mian.c中会出现如下报错:

main.c:(.text+0xc): undefined reference to`hello'

collect2: error: ld returned 1 exit status

上面报错说,在main.c中找不到hello定义。

如果在main.c文件中,将hello()函数去掉,如下

int main(int argc, const char *argv[])

{

       hello1();

 

       return0;

}

我们看一下编译情况,没有任何错误,在看一下执行结果:

hello world!

 

为什么hello函数前加static后, main.c文件就不能调用hello.c文件中的函数了呢?

 

这就是我们要说的staic第二个用法:staic修饰函数

staic修饰函数时,该函数只能被本文件的其他函数调用而不允许被其他文件的函数调用,也就是该文件的私有函数,只有该文件有独享权,这样的函数称为“内部函数”。

 

示例3

有一个工程有两个c文件,一个是hello.c文件,一个是main.c文件。

在hello.c文件中

 

#include <stdio.h>

 

int global = 12345;

 

int hello(void)

{

       printf("Thisis hello fun! global = %d\n",global);

 

       return0;

}

在main.c中

#include <stdio.h>

int hello(void);

extern global;//声明该变量在其他文件定义

 

int main(int argc, const char *argv[])

{

       printf("Thisis main fun! global = %d\n",global);

       hello();

 

       return0;

}

 

我们看一下输出结果:

This is main fun! global = 12345

This is hello fun! global = 12345

 

如果在hello.c文件中,在定义变量global前加入static,程序如下:

#include <stdio.h>

 

static int global = 12345;

 

int hello(void)

{

       printf("Thisis hello fun! global = %d\n",global);

 

       return0;

}

 

我们看一下编译情况:

在mian.c文件中出现如下错误:

main.c:(.text+0xa): undefined reference to`global'

collect2: error: ld returned 1 exit status

在mian.c找不到global定义。

如果将mian.c文件改为如下:

#include <stdio.h>

int hello(void);

 

int main(int argc, const char *argv[])

{

       hello();

 

       return0;

}

我们看一下输出结果:

This is hello fun! global = 12345

 

为什么全局变量global前加static后,main.c文件就不能使用hello.c的全局变量global了呢?

这就是我们要说的staic第三个用法:staic修饰全局变量

staic修饰全局变量后,该变量只能在本文件中使用而不允许在其他文件中使用。这个全局变量被称为静态全局变量,你可以把它当作本文件的私有变量。

 

总结

Static常用的三个用法:

1.    修饰局部变量

static 修饰局部变量时,该变量称为静态局部变量,静态局部变量的存储空间在静态存储区,与函数的堆栈区不在同一个区域,因此函数运行完毕后静态局部变量不会被释放。静态局部变量只赋一次初值。下次使用静态局部变量的时候直接使用上次保存下来的值而不会重新赋值。。

 

2.    staic修饰函数时,该函数只能被本文件的其他函数调用而不允许被其他文件的函数调用,也就是该文件的私有函数,只有该文件有独享权,这样的函数称为“内部函数”。

 

3.    staic修饰全局变量后,该变量只能在本文件中使用而不允许在其他文件中使用。这个全局变量被称为静态全局变量,你可以把它当作本文件的私有变量。

 

在本博客中需要用到以下几个gcc命令,嵌入式Linux编译器GCC如何使用这是我下一篇博客要讲的

gcc -c hello.c -o hello.o

gcc -c main.c -o main.o

gcc main.o hello.o -o main

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值