c语言存储类_C语言存储类简介

c语言存储类

储存类别 (Storage Classes )

The storage classes are generally used to specify the different features of variable/function. The features govern various aspects of the variables or function which include the scope, visibility, and lifetime, which in turn assists us in knowing the functionality of the particular variable during the runtime of the program.

存储类通常用于指定变量/函数的不同功能。 这些功能控制着变量或函数的各个方面,包括范围,可见性和生存期,这些又有助于我们在程序运行时了解特定变量的功能。

C中的存储类类型 (Types of Storage Classes in C)

There are basically 4 types of storage classes in C,

C语言基本上有4种类型的存储类别,

1)自动 (1) auto )

Any variable which is declared inside a function or block is by default assigned an auto class also called automatic variable. So it is not much necessary to separately call a variable as auto. The auto variables are only accessible within the block or function in which they are declared. In case you want o access these variables outside their scope or block, this can be done by using the concept of pointers, by pointing to the address of the variable which is to be accessed.

默认情况下,在函数或块中声明的任何变量都会被分配一个自动类,也称为自动变量。 因此,没有必要将变量分别称为auto。 自动变量只能在声明它们的块或函数中访问。 如果要o在这些变量的范围或块之外访问这些变量,可以通过使用指针的概念,指向要访问的变量的地址来完成。

2)外部 (2) extern )

Any variable which is declared outside a function is defined as an extern variable. The variable should not be defined in the same block, or else it won't be regarded as an extern variable. It is also referred to as global variable. If a variable with the same name as that of an extern variable is declared inside a block or function, then the local variable will get more preference as compared to the extern variable. As a result, extern variable will be hidden in that case.

在函数外部声明的任何变量都定义为外部变量。 该变量不应在同一块中定义,否则将不被视为外部变量。 它也称为全局变量。 如果在块或函数中声明了与外部变量同名的变量,则与外部变量相比,局部变量将具有更高的优先级。 结果,在这种情况下,extern变量将被隐藏。

3)静态 (3) static )

The main property of static variables is that they can retain their values even when they are used out of their scope. So, they are able to store the value of their last use in their scope. The static variables are initialized only once and they exist throughout the program execution. Therefore, memory is also allocated only once as there is no need to redeclare the variables. The scope of these Local Static variables are local to the block or function in which they are declared. On the other hand, the scope of Global Static variables is throughout the program execution. If a static variable is not explicitly initialized, it is assigned value 0 by the compiler.

静态变量的主要特性是,即使超出其范围使用它们也可以保留其值。 因此,他们能够在其范围内存储其最后使用的价值。 静态变量仅初始化一次,并且在整个程序执行过程中都存在。 因此,由于无需重新声明变量,因此内存也仅分配一次。 这些局部静态变量的范围对于声明它们的块或函数是局部的。 另一方面,全局静态变量的范围遍及整个程序执行。 如果未明确初始化静态变量,则编译器会将其分配为0。

4)寄存器 (4) registers )

The register storage classes have the same functionality and scope as that of auto variables, but the only difference is caused by the way the registers are stored. The compiler stores these variables in the high-speed register memory of the microprocessor. The variables which are very frequently used by the compiler are stored in register memory, as the register have very limited memory. If the register memory exceeds then the compiler automatically converts the register variable to auto variable. We can not obtain the address of register variable using pointers. The scope and lifetime of register variables are identical to the auto variables.

寄存器存储类具有与自动变量相同的功能和范围,但是唯一的区别是由寄存器的存储方式引起的。 编译器将这些变量存储在微处理器的高速寄存器存储器中。 编译器经常使用的变量存储在寄存器存储器中,因为寄存器的存储器非常有限。 如果寄存器内存超出,则编译器会自动将寄存器变量转换为自动变量。 我们无法使用指针获取寄存器变量的地址。 寄存器变量的范围和生存期与自动变量相同。

Syntax:

句法:

 storage_class var_data_type var_name;

Read: Storage classes with Examples in C programming language.

阅读: 带C编程语言示例的存储类。

翻译自: https://www.includehelp.com/c/introduction-to-storage-classes.aspx

c语言存储类

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值