C Storage Classes || C 语言存储类别

本文详细介绍了C语言中的存储类别,包括auto、extern、register、static等关键字的使用方法及应用场景,并通过具体示例帮助理解。
存储类别:
    auto
    extern
    register
    static
    
auto:

    函数(局部作用域)局部变量默认存储类型. 栈区(stack),编译器自动分配释放.
    
extern:
    全局变量默认存储类型. 程序全局(静态)区, 编译时固定分配, 程序结束释放.
    全局变量可以在不同文件中定义, 其他文件中用extern表示. 不同文件可以分开编译.
    函数全部是extern存储.
    e.g.:
        file1.c
        -------------------------------------------------------------------
        #include <stdio.h>
        int    a=1, b=2, c=3;      /* extern, global variables */
        int    f(void);            /* extern, function    */
        
        int    main(void)
        {
            printf("%3d/n", f());
            printf("%3d%3d%3d/n", a, b, c);
            return 0;
        }
        
        file2.c
        -------------------------------------------------------------------
        int    f(void)
        {
            extern    int    a;     /* compiler will look for a elsewhere */
            int            b, c;    /* auto, local variables */
            
            a = b = c = 4;
            return(a + b + c);
        }

        
register:
    建议编译器将变量储存到register里, 以加快读写速度.
    声明变量时加register只是建议编译器用register存储(a register declaration is taken only as ADVICE to the compiler), 实际存储方式由编译器依register使用状态决定.
    因此, 为尽可能使加register声明的变量在register中存储, 变量声明应该尽量靠近变量使用的位置.
    e.g.:
        {
            register int    i;
            for(i = 0; i < MAX; i++)
            {
                /* ... */
            }
        }

        
static:
    static
存储至程序全局(静态)区.
    static存储有两种基本用法. 一 函数局部静态变量, 二 限定全局变量或函数文件作用域.
    用法一:
        局部变量保留上次结果.
        e.g.:
            void f(void)
            {
                static int    cnt = 0;    /* static, local variable, private to f() */
                
                ++cnt;
                if(cnt % 2 == 0)
                {
                    /* ... */
                }
                else
                {
                    /* ... */
                }

            }
        第一次使用cnt局部变量的时候, 初始化为0. 以后每次调用f()的时候cnt不再被初始化, 而是保留上次结果.
        cnt声明为static, 保存在程序全局(静态)区, 但是仍是f()局部变量, 其他函数不能用变量名访问(可以用指针访问).
        
    用法二:
        限定全局变量或函数的作用域.
        声明全局变量时, 加static声明限定全局变量单一文件作用域.
        声明函数时, 加static声明限定单一文件作用域.
        e.g.:
        file1.c
        ---------------------------------------------------------------------
        #include<stdio.h>
        
        static int    a;            /* extern, static, private to this file */
        static int    f(void);    /* extern, static, private to this file */
        
        int    main(void)
        {
            int b;
            a = 0;
            b = f();
            printf("%d %d/n", a, b);
        
            return 0;
        }
        
        static int    f(void)
        {
            return -a;
        }
        
        file2.c
        -------------------------------------------------------------------
        #inlcude<stdio.h>
        
        int    g(void)
        {
            int c;
            extern a;        /* Error: cannot access a */
            c = f();        /* Error: cannot access function f() */
            
            return 0;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值