c语言存储类
In C language, each variable has a storage class which decides the following things:
在C语言中,每个变量都有一个存储类,该存储类决定以下内容:
scope i.e where the value of the variable would be available inside a program.
范围,即变量的值在程序内部可用的位置。
default initial value i.e if we do not explicitly initialize that variable, what will be its default initial value.
默认初始值,即如果我们不显式初始化该变量,则其默认初始值为什么。
lifetime of that variable i.e for how long will that variable exist.
该变量的寿命 ,即该变量将存在多长时间。
The following storage classes are most oftenly used in C programming,
以下存储类在C编程中最常使用,
Automatic variables
自动变量
External variables
外部变量
Static variables
静态变量
Register variables
注册变量
自动变量: auto (Automatic variables: auto)
Scope: Variable defined with auto storage class are local to the function block inside which they are defined.
范围:用auto存储类定义的变量对于在其内定义的功能块而言是局部的。
Default Initial Value: Any random value i.e garbage value.
默认初始值:任何随机值,即垃圾值。
Lifetime: Till the end of the function/method block where the variable is defined.
生命周期:直到定义变量的功能/方法块的末尾。
A variable declared inside a function without any storage class specification, is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function's execution is completed. Automatic variables can also be called local variables because they are local to a function. By default they are assigned garbage value by the compiler.
在函数内部声明而没有任何存储类规范的变量 ,默认情况下是自动变量 。 它们在调用函数时创建,并在函数执行完成时自动销毁。 自动变量也可以称为局部变量,因为它们是函数的局部变量 。 默认情况下,编译器会为它们分配垃圾值 。
#include<stdio.h>
void main()
{
int detail;
// or
auto int details; //Both are same
}
外部或全局变量 (External or Global variable)
Scope: Global i.e everywhere in the program. These variables are not bound by any function, they are available everywhere.
范围:全局,即程序中的任何地方。 这些变量不受任何功能的约束,它们随处可用。
Default initial value: 0(zero).
默认初始值: 0(零)。
Lifetime: Till the program doesn't finish its execution, you can access global variables.
生命周期:直到程序没有完成执行,您才可以访问全局变量。
A variable that is declared outside any function is a Global Variable. Global variables remain available throughout the program execution. By default, initial value of the Global variable is 0(zero). One important thing to remember about global variable is that their values can be changed by any function in the program.
在任何函数外部声明的变量是Global Variable 。 全局变量在整个程序执行期间仍然可用。 默认情况下,全局变量的初始值为0(零)。 关于全局变量要记住的一件事是程序中的任何函数都可以更改它们的值。
#include<stdio.h>
int number; // global variable
void main()
{
number = 10;
printf("I am in main function. My value is %d\n", number);
fun1(); //function calling, discussed in next topic
fun2(); //function calling, discussed in next topic
}
/* This is function 1 */
fun1()
{
number = 20;
printf("I am in function fun1. My value is %d", number);
}
/* This is function 1 */
fun2()
{
printf("\nI am in function fun2. My value is %d", number);
}
I am in function main. My value is 10 I am in function fun1. My value is 20 I am in function fun2. My value is 20
我在职能部门。 我的值是10我在fun1函数中。 我的值是20我在fun2函数中。 我的价值是20
Here the global variable number is available to all three functions and thus, if one function changes the value of the variable, it gets changed in every function.
此处,全局变量number可用于所有三个函数,因此,如果一个函数更改了变量的值,则每个函数都会更改它。
Note: Declaring the storage class as global or external for all the variables in a program can waste a lot of memory space because these variables have a lifetime till the end of the program. Thus, variables, which are not needed till the end of the program, will still occupy the memory and thus, memory will be wasted.
注意:对于程序中的所有变量,将存储类声明为全局或外部存储类型会浪费大量内存空间,因为这些变量的生命周期一直到程序结束。 因此,直到程序结束时才需要的变量仍然会占用内存,从而浪费内存。
extern关键字 (extern keyword)
The extern keyword is used with a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables.
extern关键字与变量一起使用,以通知编译器该变量在其他地方声明。 extern声明不为变量分配存储空间。
不使用extern时出现问题 (Problem when extern is not used)
int main()
{
a = 10; //Error: cannot find definition of variable 'a'
printf("%d", a);
}
在同一文件中使用extern的示例 (Example using extern in same file)
int main()
{
extern int x; //informs the compiler that it is defined somewhere else
x = 10;
printf("%d", x);
}
int x; //Global variable x
静态变量 (Static variables)
Scope: Local to the block in which the variable is defined
范围:局部于定义变量的块
Default initial value: 0(Zero).
默认初始值: 0(零)。
Lifetime: Till the whole program doesn't finish its execution.
生命周期:直到整个程序都没有完成执行。
A static variable tells the compiler to persist/save the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static variable is initialized only once and remains into existence till the end of the program. A static variable can either be internal or external depending upon the place of declaration. Scope of internal static variable remains inside the function in which it is defined. External static variables remain restricted to scope of file in which they are declared.
static变量告诉编译器将变量保留/保存到程序结束。 static变量只初始化一次并一直存在直到程序结束,而不是每次进入和超出范围时都创建和销毁变量。 static变量可以是内部变量,也可以是外部变量,具体取决于声明的位置。 内部静态变量的范围保留在定义它的函数中。 外部静态变量仍然限于声明它们的文件范围。
They are assigned 0 (zero) as default value by the compiler.
编译器将它们分配为0(零)作为默认值。
#include<stdio.h>
void test(); //Function declaration (discussed in next topic)
int main()
{
test();
test();
test();
}
void test()
{
static int a = 0; //a static variable
a = a + 1;
printf("%d\t",a);
}
1 2 3
1 2 3
注册变量 (Register variable)
Scope: Local to the function in which it is declared.
范围:局部于声明该函数的函数。
Default initial value: Any random value i.e garbage value
默认初始值:任何随机值,即垃圾值
Lifetime: Till the end of function/method block, in which the variable is defined.
生命周期:直到定义了变量的功能/方法块的结尾。
Register variables inform the compiler to store the variable in CPU register instead of memory. Register variables have faster accessibility than a normal variable. Generally, the frequently used variables are kept in registers. But only a few variables can be placed inside registers. One application of register storage class can be in using loops, where the variable gets used a number of times in the program, in a very short span of time.
寄存器变量通知编译器将变量存储在CPU寄存器中而不是内存中。 寄存器变量比普通变量具有更快的可访问性。 通常,常用变量保存在寄存器中。 但是只能将几个变量放入寄存器中。 寄存器存储类的一种应用可以是使用循环,在很短的时间内,变量在程序中被使用了很多次。
NOTE: We can never get the address of such variables.
注意:我们永远无法获得此类变量的地址。
Syntax :
句法 :
register int number;
Note: Even though we have declared the storage class of our variable number as register, we cannot surely say that the value of the variable would be stored in a register. This is because the number of registers in a CPU are limited. Also, CPU registers are meant to do a lot of important work. Thus, sometimes they may not be free. In such scenario, the variable works as if its storage class is auto.
注意:即使我们已将变量number的存储类声明为寄存器,我们也不能肯定地说变量的值将存储在寄存器中。 这是因为CPU中的寄存器数量有限。 另外,CPU寄存器还可以完成许多重要的工作。 因此,有时它们可能不是免费的。 在这种情况下,变量就像其存储类是auto 。
应该使用哪种存储类以及何时使用 (Which storage class should be used and when)
To improve the speed of execution of the program and to carefully use the memory space occupied by the variables, following points should be kept in mind while using storage classes:
为了提高程序的执行速度并仔细使用变量占用的内存空间,在使用存储类时应牢记以下几点:
We should use
staticstorage class only when we want the value of the variable to remain same every time we call it using different function calls.仅当我们希望每次使用不同的函数调用来调用变量时,变量的值保持相同时,才应使用
static存储类。We should use
registerstorage class only for those variables that are used in our program very oftenly. CPU registers are limited and thus should be used carefully.我们应该仅将
register存储类用于程序中经常使用的那些变量。 CPU寄存器受到限制,因此应谨慎使用。We should use external or global storage class only for those variables that are being used by almost all the functions in the program.
我们应该仅将外部或全局存储类用于程序中几乎所有函数正在使用的那些变量。
If we do not have the purpose of any of the above mentioned storage classes, then we should use the automatic storage class.
如果没有上述任何存储类的目的,则应使用自动存储类。
翻译自: https://www.studytonight.com/c/storage-classes-in-c.php
c语言存储类
6692

被折叠的 条评论
为什么被折叠?



