专栏中心

EEPW首页 > 专栏 > C语言存储类型

C语言存储类型

发布人:0750long 时间:2010-02-04 来源:工程师 发布文章
 
看谭浩强老师的书后一直以为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收发器的互操作性和信号完整性

视频 2010-03-17

PCB用語集(J-E-C)

资源下载 2007-03-23

十年之遥 —— 实现AGI仍存在基础性问题

2025-10-21

pcb制造工艺综述

资源下载 2007-03-23

Cadence电子设计仿真工具标准搭载村田制作所的产品数据

Gartner发布2026年十大战略技术趋势

江波龙推出业内首款集成封装mSSD,Office is Factory实现灵活、高效交付

TI用在工业里的Sigma-Delta调制器

视频 2010-03-17

新突思推出下一代Astra™多模态GenAI处理器,赋能智能物联网边缘的未来

PCB载流能力计算

选择电机控制中的位置传感器

使用1080p视频工作台,加速您的视频格式转换

视频 2010-03-17

安世半导体面临困境:全球汽车生产线或将迅速受到干扰

Power-Logic 应用培训教程

POWER PCB 工作层配置文件

分析FPGA收发器的互操作性和信号完整性

视频 2010-03-17

算力引擎驱动AI技术创新,上半年智算云基础设施市场(AI IaaS)翻倍增长

最强AI芯片首次实现「美国本土造」

在Arria II GX收发器FPGA上实现PCIe、XAUI和3G-SDI

视频 2010-03-17
更多 培训课堂
更多 焦点
更多 视频

技术专区