1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c语言存储类_C编程语言的存储类

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

时间:2020-11-19 15:38:57

相关推荐

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

c语言存储类

A variable'sstorage classtells 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;

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;

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;

Example:

例:

#include <stdio.h>void abc(){static int a=10;printf("%d\n",a);a=a+10;}int main(){abc();abc();abc();}

Output

输出量

102030

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;

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

输出量

153545457585

NOTE:Mostly in many cases preference is given to a local variable.

注意:通常在许多情况下,优先级是局部变量。

翻译自: /c/storage-classes.aspx

c语言存储类

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。