【C语言疯狂讲义】(十五)static和extern关键字使用

static extern使用

 

    static 把一个变量声明为静态变量(内部)

    extern :  把一个变量声明为外部变量

 

     1static extern 对局部变量的作用

 

        static 局部变量:延长局部变量的生命周期

 

                void sum(){

         

                    static int a=10;

                     a++;

                }

 

 

       extern 局部变量:(不能这么用)

 

 

     2static extern 对全局变量的作用

 

         main.c 包含了 test.h

 

        test.c    static int a = 10;

 

                  表示a只能在test.c中使用,表示是一个内部的变量

 

        main.c    static int a = 15;

                   printf("%d\n",a);  // 15


        test.c     int b = 100;

 

        main.c     使用b

 

                   1) main.c中声明一个同名的全局变量

                       int b;   等价于 extern  int  b;

 

                   2) main.c中需要使用 b的之前

 

                       extern  int  b;

 

     3static extern 对函数的作用

 

        static 修饰函数:内部函数,不能在其他文件中访问

        

        extern 修饰函数:函数定义的时候,默认就是extern

    

        test.c   extern int sum(int a,int b){      }

 

        main.c   extern int sum(int a,int b);

 

                 sum(1,2);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值