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语言提供了四种由存储类说明符指示的存储类:auto
、register
、extern
和static
。标识符的存储类别决定其存储持续时间、作用域和链接。标识符的存储持续时间是该标识符在内存中存在的时间段。有些标识符存在的时间很短,有些标识符会重复创建和销毁,还有一些标识符在程序的整个执行过程中都存在。标识符的作用域是可以在程序中引用标识符的作用域。一些标识符可以在整个程序中引用,而另一些标识符只能从程序的某些部分引用。对于多源文件程序(我们将在第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.
四个存储类别说明符可以分为两种存储持续时间:自动存储持续时间和静态存储持续时间。 关键字auto
和register
用于声明自动存储期限的变量。 具有自动存储期限的变量是在进入定义它们的块时创建的; 它们在块处于活动状态时存在,并且在退出块时被销毁。
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 变量 x 和 y 是自动局部变量,它们仅存在于该声明出现的函数体中:
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