c語言存儲類
A variable's storage class tells us the following,
變量的存儲類告訴我們以下內容:
Where the variables would be stored?
變量將存儲在哪里?
What will be the initial of the variable, if the initial value is not specifically assigned? (i.e. the default initial value).
如果未特別指定初始值,則變量的初始值是什么? (即默認初始值)。
What is the scope of the variables, i.e. in which part of the program of the functions the value of the variable would be available?
變量的范圍是什么,即變量的值在函數程序的哪個部分可用?
What is the life of the variable, i.e. how long the variable exists?
變量的壽命是多少,即變量存在多長時間?
C中的存儲類類型 (Types of storage classes in C)
There are four classes in C programming language,
C編程語言共有四類,
Automatic storage classes
自動存儲類
Register storage classes
注冊存儲類別
Static storage classes
靜態存儲類
External storage classes
外部存儲類別
1)自動存儲類 (1) Automatic storage classes)
The keyword auto is used to declare variable of automatic storage class. (keyword auto is optional).
關鍵字auto用于聲明自動存儲類的變量。 (關鍵字auto是可選的)。
Syntax
句法
auto int a;
int a;
Storage | Memory |
---|---|
Default initial value | Unpredictable value |
Scope | Local to the block in which the variable is defined. |
Life | Control remains within the block in which the variable is defined. |
存儲 | 記憶 |
---|---|
默認初始值 | 不可預測的價值 |
范圍 | 局部于定義變量的塊。 |
生活 | 控制保留在定義變量的塊內。 |
Example: To display the default values
示例:顯示默認值
#include <stdio.h>
int main ()
{
auto int i,j;
printf("\n%d %d",i,j);
return 0;
}
NOTE: In the output two garbage values will be displayed as automatic variable by default store garbage values.
注意:在輸出中,默認情況下,存儲垃圾值將顯示兩個垃圾值作為自動變量。
2)注冊存儲類 (2) Register storage classes)
The keyword register is used to declare a variable of the register storage class.
關鍵字register用于聲明寄存器存儲類的變量。
Syntax
句法
register int a;
Storage | CPU Register |
---|---|
Default initial value | Garbage value |
Scope | Local to the block in which the variable is defined. |
Life | Control remains within the block in which the variable is defined. |
存儲 | CPU寄存器 |
---|---|
默認初始值 | 垃圾價值 |
范圍 | 局部于定義變量的塊。 |
生活 | 控制保留在定義變量的塊內。 |
Example:
例:
#include <stdio.h>
int main()
{
register int i;
for(i=1;i<=100;i++);
printf("%d",i);
return 0;
}
Output
輸出量
101
NOTE: A variable stored in CPU register can always be accessed faster than the one which is stored in memory.
注意:存儲在CPU寄存器中的變量始終可以比存儲在存儲器中的變量更快地訪問。
We cannot use register storage class for all types of variables.
我們不能將寄存器存儲類用于所有類型的變量。
Example: register double a; ,register float c; etc.
示例:注冊double a; ,注冊float c; 等等
3)靜態存儲類 (3) Static storage classes)
The keyword static is used to declare variables of static storage class.
關鍵字static用于聲明靜態存儲類的變量。
Syntax
句法
static int i;
Storage | Memory |
---|---|
Default initial value | 0 |
Scope | Local to the block in which the variable is defined. |
Life | Value of the variable remains b/w different function calls. |
存儲 | 記憶 |
---|---|
默認初始值 | 0 |
范圍 | 局部于定義變量的塊。 |
生活 | 變量的值仍然是黑白不同的函數調用。 |
Example:
例:
#include <stdio.h>
void abc()
{
static int a=10;
printf("%d\n",a);
a=a+10;
}
int main()
{
abc();
abc();
abc();
}
Output
輸出量
10
20
30
NOTE: We should avoid using static variables unless we really need them. Because their value are kept in memory when the variables are not active, which means they take up space in memory that could otherwise be used by other variables.
注意:我們應該避免使用靜態變量,除非我們確實需要它們。 因為當變量不活動時它們的值會保留在內存中,這意味著它們會占用內存中的空間,否則這些空間可能會被其他變量使用。
4)外部存儲類 (4) External storage classes)
The keyword extern is used to declare the variables of external storage class.
關鍵字extern用于聲明外部存儲類的變量。
In this the variable declared without keyword but it should be defined above the function(outside).
在這種情況下,聲明為不帶關鍵字的變量,但應在函數(外部)上方定義。
Syntax
句法
extern int i;
Storage | Memory |
---|---|
Default initial value | 0 |
Scope | Global |
Life | As long as the program execution does not come to an end. |
存儲 | 記憶 |
---|---|
默認初始值 | 0 |
范圍 | 全球 |
生活 | 只要程序執行沒有結束。 |
Example:
例:
#include <stdio.h>
/*Global variable*/
int a=10;
void abc()
{
printf("%d\n",a);
a=a+10;
}
int main()
{
a=a+5;
printf("%d\n",a);
a=a+20;
abc();
printf("%d\n",a);
abc();
a=a+20;
abc();
printf("%d\n",a);
return 0;
}
Output
輸出量
15
35
45
45
75
85
NOTE: Mostly in many cases preference is given to a local variable.
注意:通常在許多情況下,優先級是局部變量。
翻譯自: https://www.includehelp.com/c/storage-classes.aspx
c語言存儲類