【C语言】变量的存储类型和作用域;关键词auto, register, extern和static

本文详细介绍了C语言中的四种存储类别——auto、register、extern和static,以及它们对变量存储持续时间、作用域和链接的影响。自动存储的变量在进入块时创建,退出时销毁,register关键字建议编译器使用高速硬件寄存器,但编译器可能忽略。静态存储的变量在整个程序执行期间存在,其中static局部变量在函数退出时保留其值。此外,文章还探讨了标识符的作用域,包括函数作用域、文件作用域和块作用域,以及如何在多源文件程序中处理全局变量和函数的链接问题。
摘要由CSDN通过智能技术生成

5.11 Storage Classes

In Chapters 2 through 4, we used identifiers for variable names. The attributes of variables include name, type, size and value. In this Chapter, we also use identifiers as names for user defined functions. Actually, each identifier in a program has other attributes including storage class, storage duration, scope and linkage.

在第2章到第4章中,我们使用变量名的标识符。变量的属性包括名称、类型、大小和值。在本章中,我们还使用标识符作为用户定义函数的名称。实际上,程序中的每个标识符还有其他属性,包括存储类别存储持续时间作用域链接

C provides four storage classes indicated by the storage class specifiers: auto, register, extern and static. An identifier’s storage class determines its storage duration, scope and linkage. An identifier’s storage duration is the period during which that identifier exists in memory. Some identifiers exist briefly, some are repeatedly created and destroyed, and others exist for the entire execution of a program. An identifier’s scope is where the identifier can be referenced in a program. Some identifiers can be referenced throughout a program, others from only portions of a program. An identifier’s linkage determines for a multiple-source-file program (a topic we will investigate in Chapter 14) whether the identifier is known only in the current source file or in any source file with proper declarations. This section discussed the four storage classes and storage duration. Section 5.12 discusses the scope of identifiers. Chapter 14, Other C topics, discusses identifier linkage and programming with multiple source files.

C语言提供了四种由存储类说明符指示的存储类:autoregisterexternstatic。标识符的存储类别决定其存储持续时间、作用域和链接。标识符的存储持续时间是该标识符在内存中存在的时间段。有些标识符存在的时间很短,有些标识符会重复创建和销毁,还有一些标识符在程序的整个执行过程中都存在。标识符的作用域是可以在程序中引用标识符的作用域。一些标识符可以在整个程序中引用,而另一些标识符只能从程序的某些部分引用。对于多源文件程序(我们将在第14章中研究的主题),标识符的链接确定标识符是否仅在当前源文件中或在具有正确声明的任何源文件中已知。本节讨论四种存储类别和存储持续时间。5.12节讨论了标识符的作用域。第14章,其他C语言主题,讨论标识符链接和多个源文件的编程。

The four storage class specifiers can be split into two storage durations: automatic storage duration and static storage duration. Keywords auto and register are used to declare variables of automatic storage duration. Variables with automatic storage duration are created when the block in which they are defined is entered; they exist while the block is active, and they are destroyed when the block is exited.

四个存储类别说明符可以分为两种存储持续时间:自动存储持续时间和静态存储持续时间。 关键字autoregister用于声明自动存储期限的变量。 具有自动存储期限的变量是在进入定义它们的块时创建的; 它们在块处于活动状态时存在,并且在退出块时被销毁。

Only variables can have automatic storage duration. A function’s local variables(those declared in the parameter list or in the function body) normally have automatic storage duration. Keyword auto explicitly declares variables of automatic storage duration. For example, the following declaration indicates that double variables x and y are automatic local variables and they exist only in the body of the function in which the declaration appears:

只有变量可以有自动存储期限。函数的局部变量(在参数列表或函数体中声明的变量)通常具有自动存储期限。关键字auto显式声明自动存储持续时间的变量。例如,以下声明表明 double 变量 xy 是自动局部变量,它们仅存在于该声明出现的函数体中:

auto double x, y;

Local variables have automatic storage duration by default, so keyword auto is rarely used. For the remainder of the text, we will refer to variables with automatic storage duration simply as automatic variables.

局部变量默认具有自动存储期限,因此很少使用关键字auto。在本文的其余部分中,我们将具有自动存储持续时间的变量简称为自动变量

Performance Tip 5.1

Automatic storage is a means of conserving memory because automatic variables exist only when they are needed. They are created wh

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值