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

本文详细介绍了C语言中的存储类型,包括auto、register、static和extern等,解释了它们如何影响变量的作用域和生命周期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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定义一个函数时,这个函数对其他文件可见。


 

 

资源下载链接为: https://pan.quark.cn/s/abbae039bf2a 无锡平芯微半导体科技有限公司生产的A1SHB三极管(全称PW2301A)是一款P沟道增强型MOSFET,具备低内阻、高重复雪崩耐受能力以及高效电源切换设计等优势。其技术规格如下:最大漏源电压(VDS)为-20V,最大连续漏极电流(ID)为-3A,可在此条件下稳定工作;栅源电压(VGS)最大值为±12V,能承受正反向电压;脉冲漏极电流(IDM)可达-10A,适合处理短暂高电流脉冲;最大功率耗散(PD)为1W,可防止器件过热。A1SHB采用3引脚SOT23-3封装,小型化设计利于空间受限的应用场景。热特性方面,结到环境的热阻(RθJA)为125℃/W,即每增加1W功率损耗,结温上升125℃,提示设计电路时需考虑散热。 A1SHB的电气性能出色,开关特性优异。开关测试电路及波形图(图1、图2)展示了不同条件下的开关性能,包括开关上升时间(tr)、下降时间(tf)、开启时间(ton)和关闭时间(toff),这些参数对评估MOSFET在高频开关应用中的效率至关重要。图4呈现了漏极电流(ID)与漏源电压(VDS)的关系,图5描绘了输出特性曲线,反映不同栅源电压下漏极电流的变化。图6至图10进一步揭示性能特征:转移特性(图7)显示栅极电压(Vgs)对漏极电流的影响;漏源开态电阻(RDS(ON))随Vgs变化的曲线(图8、图9)展现不同控制电压下的阻抗;图10可能涉及电容特性,对开关操作的响应速度和稳定性有重要影响。 A1SHB三极管(PW2301A)是高性能P沟道MOSFET,适用于低内阻、高效率电源切换及其他多种应用。用户在设计电路时,需充分考虑其电气参数、封装尺寸及热管理,以确保器件的可靠性和长期稳定性。无锡平芯微半导体科技有限公司提供的技术支持和代理商服务,可为用户在产品选型和应用过程中提供有
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值