看谭浩强老师的书后一直以为C语言的存储类型(storage class)有四种(auto/static/register/external),今天看了《The C Programming Language·2nd Edition》后面所附的参考手册后,才发现以前的想法是错误的。根据这个手册对C89标准的解释,C语言的存储类型只有两种:动态的(automatic)和静态的(static)。
动态的变量是某个块的局部变量,当程序从这个块中退出时变量也自动撤销。如果没有特别声明(或者用auto关键字声明),变量默认是动态的。用关键字register声明的变量也是动态的,只是这个变量可能会被放在寄存器中。
静态的变量可能局部于某个块也可能扩展到所有块,无论是退出或重新进入某个程序块它的值都将保留。在程序块中,静态的变量用static声明。不在任何一个程序块内而和函数声明同级别的变量也是静态的。如果想使它们仅作用于某个特定的翻译单元(a particular translation unit)内(即文件范围),则用static声明,这样会使它们具有内在的联接(internal linkage);如果忽略存储类型或用external声明,则使它们具有外部的联接(external linkage),这样它们就具有全局作用域。
参考资料(摘自《The C Programming Language·2nd Edition》P195,北京,机械工业出版社,2006.8):
There are two storage classes: automatic and static. Several keywords, together with the context of an object's declaration, specify its storage class. Automatic objects are local to a block, and are discarded on exit from the block. Declarations within a block create automatic objects if no storage class specification is mentioned, or if the auto specifier is used. Objects declared register are automatic, and are (if possible) stored in fast registers of the machine.
Static objects may be local to a block or external to all blocks, but in either case retain their values across exit from and reentry to functions and blocks. Within a block, including a block that provides the code for a function, static objects are declared with the keyword static. The objects declared outside all blocks, at the same level as function definitions, are always static. They may be make local to a particular translation unit by use of the static keyword; this gives them internal linkage. They become global to an entire program by omitting an explicit storage class, or by using the keyord extern; this gives them external linkage.
相关推荐
数智基建推动自主可控人工智能革命的两大举措
分析FPGA收发器的互操作性和信号完整性
PCB用語集(J-E-C)
十年之遥 —— 实现AGI仍存在基础性问题
SAA3008红外线遥控发射电路
pcb制造工艺综述
Cadence电子设计仿真工具标准搭载村田制作所的产品数据
SC41343(通用)红外线、超声波或射频遥控发射编码电路
Gartner发布2026年十大战略技术趋势
江波龙推出业内首款集成封装mSSD,Office is Factory实现灵活、高效交付
TI用在工业里的Sigma-Delta调制器
新突思推出下一代Astra™多模态GenAI处理器,赋能智能物联网边缘的未来
PCB载流能力计算
选择电机控制中的位置传感器
专家认为:全球TFT面板明年上半年可能缺货
SC41342(通用)红外线、超声波或射频遥控发射编码电路
索尼发表支持USB2.0的小型存贮介质
使用1080p视频工作台,加速您的视频格式转换
SAA3027(通用)红外线遥控发射电路
安世半导体面临困境:全球汽车生产线或将迅速受到干扰
Power-Logic 应用培训教程
POWER PCB 工作层配置文件
毕业生就业:IT业遇空前寒流 汽车制造最火爆
分析FPGA收发器的互操作性和信号完整性
算力引擎驱动AI技术创新,上半年智算云基础设施市场(AI IaaS)翻倍增长
大唐拟分拆赴港美同步上市 融资前景不明
SAA3010(电视机)红外线遥控发射电路
DVD专利费范围扩大 刻录硬件也要收专利费
最强AI芯片首次实现「美国本土造」
在Arria II GX收发器FPGA上实现PCIe、XAUI和3G-SDI