C Storage Classes-C语言的存储类型


C Storage Classes.

                   -Martin Leslie

http://gd.tuwien.ac.at/languages/c/cref-mleslie/cref.html


C has a concept of 'Storage classes' which are used to define thescope (visability) and life time of variables and/or functions.

C语言中有一个“存储类型”的概念。存储类型用于定义变量和函数的范围和生命周期。

So what Storage Classes are available?

那么都有什么样的存储类型呢?

auto

register

static

extern


auto - storage class

auto is the default storage classfor local variables.

        {

            int Count;

            auto int Month;

        }

The example above defines twovariables with the same storage class. auto can only be used within functions,i.e. local variables.

 

局部变量(local variables)的默认存储类型是auto存储类型.

        {

           int Count;

           auto int Month;

        }

上面的例子中定义的2个变量(Count和Month)拥有相同的存储类型。

auto可以用于函数中,也就是局部变量。


register - Storage Class

register is used to definelocal variables that should be stored in a register instead of RAM. This meansthat the variable has a maximum size equal to the register size (usually oneword) and cant have the unary '&' operator applied to it (as it does nothave a memory location).

        {

         register int  Miles;

        }

Register shouldonly be used for variables that require quick access - such as counters. Itshould also be noted that defining 'register' goes not mean that the variablewill be stored in a register. It means that it MIGHT be stored in a register -depending on hardware and implimentation restrictions.

 

register用于定义一个局部变量应该存储在CPU的寄存器中不是内存中(只是建议编译器这样做). 这意味着寄存器变量的最大size等于寄存器的size (通常是一个字) ,并且不能使用一元操作符 '&'取寄存器变量的地址(因为寄存器在CPU中,没有内存地址).

        {

         register int  Miles;

        }

寄存器存储类型应该用于需要被快速访问的变量– 例如用于计数器. 另外,寄存器存储类型不意味着这个变量一定会存储在寄存器中,只是可能被存储在寄存器中,这取决来自于硬件和实现(编译器实现register?)的限制。


Static - Storage Class

Click here for static functions

Static is the default storage classfor global variables. The two variables below (countand road) both have a staticstorage class.

Static 是全局变量的默认存储类型。下面的2个变量(countroad)都是静态存储类型(这里的static指的是静态这一概念,而非关键字static)。

 

     static int Count;

     int Road;

 

     main()

     {

       printf("%d\n", Count);

       printf("%d\n", Road);

     }

 

'static' can also be defined within a function. If this is done, thevariable is initalised at compilation time and retains its value between calls.Because it is initialsed at compilation time, the initalistation value must bea constant. This is serious stuff - tread with care.

'static'类型变量也可以定义在函数内,也就是静态局部变量。静态局部变量在编译时就完成了初始化,并且在函数调用时,变量的值一直保持着。

 

     void Func(void)

     {

       static Count=1;

     }

 

There is one very important use for 'static'. Considerthis bit of code.

'static'有一个非常重要的应用.思考下面这段代码.

 

     char *Func(void);

 

     main()

     {

       char *Text1;

       Text1 = Func();

     }

 

     char *Func(void)

     {

       char Text2[10]="martin";

       return(Text2);

     }

 

'Func' returns a pointer to the memory location where'Text2' starts BUT Text2 has a storage class ofauto and will disappear when we exit the functionand could be overwritten by something else. The answer is to specify:

'Func'函数返回'Text2'所在内存起始位置的指针。但是Text2的存储类型为auto,当函数退出时Text2就消亡了,原先所在的内存位置可能被重写了。

正确的声明方式如下:

 

     static char Text[10]="martin";

 

The storage assigned to 'Text2' will remain reserved for the duration ifthe program.

分配给'Text2'的内存在程序的生命周期内会一直保留。

When specified on a function declaration, it makes the function local tothe file.

当static定义一个函数时,这个函数是在当前文件可见,其他文件无法访问。


extern - storage Class

extern defines a globalvariable that is visable to ALL object modules. When you use 'extern' thevariable cannot be initalized as all it does is point the variable name at astorage location that has been previously defined.

extern声明一个全局变量对所有的目标模块可见.当你'extern' 一个变量时,这个变量不能初始化,因为'extern'所做的事就是把变量名指向之前定义的存储器位置(也就是告诉编译器,这个变量已经存在了)。

 

 

        Source2                              Source 1

        --------                              --------

 

 

        extern int count;                     int count=5;

 

        write()                               main()

        {                                     {

          printf("count is %d\n",count);       write();

        }                                     }

Count in 'source1' will have a value of 5. If source 1 changes the value of count - source 2will see the new value. Here are some example source files.

'source1'中的count的值为5. 如果source1 中改变了count 的值source 2 中会得到新的值.

 

当extern定义一个函数时,这个函数对其他文件可见。


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值