c语言存储类_C编程语言的存储类

c语言存储类

A variable's storage class tells us the following,

变量的存储类告诉我们以下内容:

  1. Where the variables would be stored?

    变量将存储在哪里?

  2. What will be the initial of the variable, if the initial value is not specifically assigned? (i.e. the default initial value).

    如果未特别指定初始值,则变量的初始值是什么? (即默认初始值)。

  3. 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?

    变量的范围是什么,即变量的值在函数程序的哪个部分可用?

  4. 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编程语言共有四类,

  1. Automatic storage classes

    自动存储类

  2. Register storage classes

    注册存储类别

  3. Static storage classes

    静态存储类

  4. 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语言存储类

你可能感兴趣的:(c语言存储类_C编程语言的存储类)