C语言中的存储类

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

null

图片[1]-C语言中的存储类-yiteyi-C++库

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

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

storage_class var_data_type var_name; 

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

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 ( "Demonstrating auto class" );
// 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" ,
a);
printf ( "--------------------------------" );
}
void registerStorageClass()
{
printf ( "Demonstrating register class" );
// declaring a register variable
register char b = 'G' ;
// printing the register variable 'b'
printf ( "Value of the variable 'b'"
" declared as register: %d" ,
b);
printf ( "--------------------------------" );
}
void externStorageClass()
{
printf ( "Demonstrating extern class" );
// 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" ,
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" ,
x);
printf ( "--------------------------------" );
}
void staticStorageClass()
{
int i = 0;
printf ( "Demonstrating static class" );
// using a static variable 'y'
printf ( "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'" );
printf ( "Loop started:" );
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 ( "The value of 'y', "
"declared as static, in %d "
"iteration is %d" ,
i, y);
// printing value of p at each iteration
printf ( "The value of non-static variable 'p', "
"in %d iteration is %d" ,
i, p);
}
printf ( "Loop ended:" );
printf ( "--------------------------------" );
}
int main()
{
printf ( "A program to demonstrate"
" Storage Classes in C" );
// 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 ( "Storage Classes demonstrated" );
return 0;
}
// This code is improved by RishabhPrabhu


输出: 用C语言演示存储类的程序 演示汽车课程 声明为自动的变量“a”的值:32 ——————————– 示范注册班 声明为寄存器的变量“b”的值:71 ——————————– 演示外部课程 声明为extern的变量“x”的值:0 声明为extern的变量“x”的修改值:2 ——————————– 演示静态课堂 在循环中将“y”声明为静态。 但是这个声明只会出现一次,因为“y”是静态的。 如果不是,那么每次“y”的值都将是声明的值5,就像变量“p”的情况一样 循环开始: 在1次迭代中,声明为静态的“y”的值为6 在1次迭代中,非静态变量“p”的值为11 在2次迭代中,声明为静态的“y”的值为7 在两次迭代中,非静态变量“p”的值为11 在3次迭代中,声明为静态的“y”的值为8 在3次迭代中,非静态变量“p”的值为11 在4次迭代中,声明为静态的“y”的值为9 在4次迭代中,非静态变量“p”的值为11 循环结束: ——————————– 存储类演示
存储类问答游戏 本文由Ayush Jaggi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享