Cprogramming 简明教程
Storage Classes in C
C storage classes define the scope (visibility) and lifetime of variables and/or functions within a C Program. They precede the type that they modify.
我们在 C 程序中有四个不同的存储类 −
We have four different storage classes in a C program −
-
auto
-
register
-
static
-
extern
The auto Storage Class
auto 是在函数或块中声明的所有变量的默认存储类。可以将关键字“ auto ”(这是可选的)用于定义局部变量。
The auto is a default storage class for all variables that are declared inside a function or a block. The keyword "auto", which is optional, can be used to define local variables.
auto 变量的范围和生存期在其声明的同一个块中。
The scope and lifetime of auto variables are within the same block in which they are declared.
Example of auto Storage Class
以下代码示例演示了自动 (auto) 变量的声明 −
The following code statements demonstrate the declaration of an automatic (auto) variable −
{
int mount;
auto int month;
}
上面的示例在同一存储类中定义了两个变量。“auto” 只可在函数中使用,即局部变量。
The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.
The register Storage Class
register 存储类用来定义应存储在寄存器中而不是 RAM 中的局部变量。这意味着该变量的最大大小等于寄存器大小(通常一个字),并且不能应用一元运算符 “&”(因为它没有存储空间)。
The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can’t have the unary '&' operator applied to it (as it does not have a memory location).
此寄存器应仅用于需要快速访问的变量,如计数器。还应注意,定义“寄存器”并不意味着变量将存储在寄存器中。这意味着,根据硬件和实现限制,它可能被存储在寄存器中。
The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.
The static Storage Class
static 存储类指示 compiler 在程序生命周期中保留局部变量,而不是每次进入和退出作用域时都创建和销毁局部变量。因此,使局部变量成为静态变量允许它们在函数调用之间保持其值。
The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.
static 修饰符还可以应用于 global variables 。执行此操作会使该变量的范围限制为在其声明的文件中。
The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared.
在 C 编程中,当 static 用于全局变量时,它会只让类的所有对象共享该成员的一个副本。
In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.
Example of static Storage Class
以下示例演示了在 C 程序中使用静态存储类的情况 −
The following example demonstrates the use of a static storage class in a C program −
#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main(){
while(count--) {
func();
}
return 0;
}
/* function definition */
void func(void) {
static int i = 5; /* local static variable */
i++;
printf("i is %d and count is %d\n", i, count);
}
Output
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
The extern Storage Class
extern 存储类用于给出全局变量的一个引用,所有程序文件都可以看到。当你使用 'extern' 时,该变量不能被初始化,然而它将变量名指向一个之前定义的存储位置。
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
当你有多个文件,并且你定义了一个全局变量或函数,并且其他文件也将使用它时,那么 extern 将用于另一个文件,以提供已定义变量或函数的引用。仅仅为了理解,extern 用于在另一个文件中声明一个全局变量或函数。
When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.
最通常在两个或更多文件共享相同全局变量或函数时使用 extern 修饰符,如下文所述。
The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
Example of extern Storage Class
外存储类的示例可能包含两个或更多文件。以下示例演示了在 C 语言中使用 extern 存储类的情况 −
The example of an extern storage class may contain two or more files. Here is an example demonstrating the use of an extern storage class in C language −
First File: main.c
First File: main.c
#include <stdio.h>
int count;
extern void write_extern();
main(){
count = 5;
write_extern();
}
Second File: support.c
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("Count is %d\n", count);
}
在此处, extern 用于在第二个文件中声明 count ,而其定义在第一个文件中 (main.c)。现在,按如下方式编译这两个文件 −
Here, extern is being used to declare count in the second file, whereas it has its definition in the first file (main.c). Now, compile these two files as follows −
$gcc main.c support.c
它将生成可执行程序 a.out 。当执行此程序时,它将产生以下输出 −
It will produce the executable program a.out. When this program is executed, it will produce the following output −
Count is 5
Use of storage classes
存储类用于定义变量的作用域、可见性、生命周期和初始(默认)值。
Storage classes are used to define the scope, visibility, lifetime, and initial (default) value of a variable.
Summary of Storage Classes
下表总结了具有不同存储类的变量的作用域、默认值和生命周期 −
The following table provides a summary of the scope, default value, and lifetime of variables having different storage classes −
Storage Class |
Name |
Memory |
Scope, Default Value |
Lifetime |
auto |
Automatic |
Internal Memory |
Local Scope, Garbage Value |
Within the same function or block in which they are declared. |
register |
Register |
Register |
Local Scope, 0 |
Within the same function or block in which they are declared. |
static |
Static |
Internal Memory |
Local Scope, 0 |
Within the program i.e., as long as program is running. |
extern |
External |
Internal Memory |
Global Scope, 0 |
Within the program i.e., as long as program is running. |