1. 主页
  2. 文档
  3. C语言教程
  4. C语言存储类
  5. C 中的存储类

C 中的存储类

存储类用于描述变量/函数的特性。这些特性基本上包括范围、可见性和生命周期,它们帮助我们在程序运行期间跟踪特定变量的存在。
C语言使用4个存储类,分别是:

  1. auto:这是在函数或块中声明的所有变量的默认存储类。因此,在用 C 语言编写程序时,很少使用关键字 auto。自动变量只能在它们已声明的块/函数内访问,而不能在它们之外(定义它们的范围)访问。当然,这些可以在声明自动变量的父块/函数内的嵌套块内访问。但是,也可以使用此处给出的指针概念,通过指向变量所在的非常精确的内存位置,在其范围之外访问它们。每当声明它们时,默认情况下都会为它们分配一个垃圾值。 
  2. extern :外部存储类只是告诉我们该变量是在别处定义的,而不是在使用它的同一块内。基本上,该值在不同的块中分配给它,并且也可以在不同的块中覆盖/更改。因此,外部变量只不过是一个用合法值初始化的全局变量,在该变量中声明它以便在其他地方使用。它可以在任何功能/块中访问。此外,通过在任何函数/块中的声明/定义之前放置“extern”关键字,也可以使普通全局变量成为外部变量。这基本上意味着我们没有初始化一个新变量,而是我们只使用/访问全局变量。使用外部变量的主要目的是可以在作为大型程序一部分的两个不同文件之间访问它们。
  3. static:该存储类用于声明静态变量,这些变量在用 C 语言编写程序时普遍使用。静态变量具有保留其值的属性,即使它们超出了它们的范围!因此,静态变量在其作用域内保留了它们最后一次使用的值。所以我们可以说它们只被初始化一次并且一直存在到程序终止。因此,没有新的内存被分配,因为它们没有被重新声明。它们的作用域是定义它们的函数的本地范围。全局静态变量可以在程序的任何地方访问。默认情况下,编译器为它们分配值 0。 
  4. register:该存储类声明了与 auto 变量具有相同功能的寄存器变量。唯一的区别是,如果有可用的空闲寄存器,编译器会尝试将这些变量存储在微处理器的寄存器中。这使得寄存器变量的使用比程序运行时存储在内存中的变量快得多。如果没有可用的寄存器,则这些寄存器只存储在内存中。通常在程序中经常访问的变量很少使用 register 关键字声明,这可以提高程序的运行时间。这里要注意的一个重要且有趣的点是,我们无法使用指针获取寄存器变量的地址。 

要为变量指定存储类,应遵循以下语法:
语法: 

storage_class var_data_type var_name; 

函数遵循与上述变量相同的语法。请查看以下 C 示例以获得进一步说明: 

// A C program to demonstrate different storage
// classes
#include <stdio.h>

// declaring the variable which is to be made extern
// an initial value can also be initialized to x
int x;

void autoStorageClass()
{

printf("\nDemonstrating auto class\n\n");

// declaring an auto variable (simply
// writing "int a=32;" works as well)
auto int a = 32;

// printing the auto variable 'a'
printf("Value of the variable 'a'"
" declared as auto: %d\n",
a);

printf("--------------------------------");
}

void registerStorageClass()
{

printf("\nDemonstrating register class\n\n");

// declaring a register variable
register char b = 'G';

// printing the register variable 'b'
printf("Value of the variable 'b'"
" declared as register: %d\n",
b);

printf("--------------------------------");
}

void externStorageClass()
{

printf("\nDemonstrating extern class\n\n");

// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;

// printing the extern variables 'x'
printf("Value of the variable 'x'"
" declared as extern: %d\n",
x);

// value of extern variable x modified
x = 2;

// printing the modified values of
// extern variables 'x'
printf("Modified value of the variable 'x'"
" declared as extern: %d\n",
x);

printf("--------------------------------");
}

void staticStorageClass()
{
int i = 0;

printf("\nDemonstrating static class\n\n");

// using a static variable 'y'
printf("Declaring 'y' as static inside the loop.\n"
"But this declaration will occur only"
" once as 'y' is static.\n"
"If not, then every time the value of 'y' "
"will be the declared value 5"
" as in the case of variable 'p'\n");

printf("\nLoop started:\n");

for (i = 1; i < 5; i++) {

// Declaring the static variable 'y'
static int y = 5;

// Declare a non-static variable 'p'
int p = 10;

// Incrementing the value of y and p by 1
y++;
p++;

// printing value of y at each iteration
printf("\nThe value of 'y', "
"declared as static, in %d "
"iteration is %d\n",
i, y);

// printing value of p at each iteration
printf("The value of non-static variable 'p', "
"in %d iteration is %d\n",
i, p);
}

printf("\nLoop ended:\n");

printf("--------------------------------");
}

int main()
{

printf("A program to demonstrate"
" Storage Classes in C\n\n");

// To demonstrate auto Storage Class
autoStorageClass();

// To demonstrate register Storage Class
registerStorageClass();

// To demonstrate extern Storage Class
externStorageClass();

// To demonstrate static Storage Class
staticStorageClass();

// exiting
printf("\n\nStorage Classes demonstrated");

return 0;
}

输出: 

A program to demonstrate Storage Classes in C
Demonstrating auto class
Value of the variable ‘a’ declared as auto: 32 
——————————– 
Demonstrating register class
Value of the variable ‘b’ declared as register: 71 
——————————– 
Demonstrating extern class
Value of the variable ‘x’ declared as extern: 0 
Modified value of the variable ‘x’ declared as extern: 2 
——————————– 
Demonstrating static class
Declaring ‘y’ as static inside the loop. 
But this declaration will occur only once as ‘y’ is static. 
If not, then every time the value of ‘y’ will be the declared value 5 as in the case of variable ‘p’
Loop started:
The value of ‘y’, declared as static, in 1 iteration is 6 
The value of non-static variable ‘p’, in 1 iteration is 11
The value of ‘y’, declared as static, in 2 iteration is 7 
The value of non-static variable ‘p’, in 2 iteration is 11
The value of ‘y’, declared as static, in 3 iteration is 8 
The value of non-static variable ‘p’, in 3 iteration is 11
The value of ‘y’, declared as static, in 4 iteration is 9 
The value of non-static variable ‘p’, in 4 iteration is 11
Loop ended: 
——————————–
Storage Classes demonstrated 

 

 

这篇文章对您有用吗?