ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

第11章:C语言存储类

前开发者教程 2020年4月9日

存储类用于了解变量的范围和生存期。有两种类型的存储类别。

  1. 本地
  2. 全球

 

以下是不同的存储类说明符:

  1. 汽车
  2. 寄存器
  3. 静态的
  4. 外部

 

在研究存储类之前,我们将讨论变量及其在不同块中的行为。 C中的一个块是写在“ {…}”花括号内的语句集。

以下是C语言中可用的不同类型的块:

  1. 功能块。
  2. 文件块。
  3. 全局块。
  4. 嵌套块。

 

1.自动变量:

在函数内部声明的任何变量将属于自动存储类。“auto” is the keyword used to declare a variable as a 汽车matic storage class.

 

  1. 自动变量存储在堆栈段中。
  2. 它们的范围在定义它们的块中。
  3. 它们的寿命直到功能执行为止。
  4. 如果未定义,它们的默认值将是垃圾。

 

例:

汽车 int i = 10;

int j = 20;

例:

#include<stdio.h>
int main(int argc, char const *argv[])
{
	
	int num = 10;

	{
		int num = 20;
		printf("The num value inside nested block = %d \n", num );
	}
		printf("The num value inside main block = %d \n", num );

	return 0;
}

输出:

The num value inside nested block = 20
The num value inside main block = 10

 

2.注册说明符:

“register” is the keyword used to declare a variable as a 寄存器 storage class. A variable declared as a 寄存器, is stored in the computer 寄存器 memory. However, this is just a request to the compiler to store the value in a 寄存器 and is not guaranteed that it will be stored there.

 

  1. Register variables are stores in the computer 寄存器 if available.
  2. 它们的范围在定义它们的块中。
  3. 它们的寿命一直到该块执行为止。
  4. 如果未定义,它们的默认值将是垃圾。

 

例:

寄存器 int i = 10;

 

3.静态说明符:

“static” is the keyword used to declare a variable as a 静态的 storage class.

 

  1. 静态的 variables are stores in the data segment.
  2. 它们的寿命一直到程序执行为止。
  3. 如果未定义,则其默认值为零。
  4. 如果将局部变量声明为静态变量,则该变量将在函数调用之间保持其值。
  5. 如果全局变量声明为静态变量,则该变量范围就是文件的范围。不能从其他文件访问该变量。

 

例:

静态的 int i = 10;

 

程序示例:

 #include<stdio.h>

static int num;

void func_1()
{
	static int called= 1;
	printf("This function is called %d times\n",called );
	called++;
}

int main(int argc, char const *argv[])
{
	printf("The global value of 静态的 variable is %d\n", num);	

	func_1();
	func_1();
	func_1();
	func_1();

	return 0;
}

 

输出:

The global value of 静态的 variable is 0
This function is called 1 times
This function is called 2 times
This function is called 3 times
This function is called 4 times

 

4.外部说明符:

“extern” is the keyword used to declare a variable as an 外部. It is an indication for the programmer that the variable is defined in other place, and it is using a reference. Initialization of the variable cannot be done, because it has been defined and initialized in other file.

 

  1. 外部 variables are stores in the data segment.
  2. 它们的寿命一直到程序执行为止。
  3. 如果未定义,则其默认值为零。
  4. 范围在已导入变量的文件中。

 

例:

外部 int i = 10;

 

编程实例:

1.c

#include<stdio.h>

extern void	func_1();

int num;

int main(int argc, char const *argv[])
{
	printf("In the main function, global value of variable is %d\n", num);	
	func_1();
	
	return 0;
}

 

2.c

#include<stdio.h>

extern int num;

int func_1()
{
	printf("In the file 2, in finc_1 function, value of 外部 variable is %d\n", num);	

	// initialization is wrong, but you can change the value of that variable.
	num += 20;
	printf("In the file 2, in finc_1 function, value of 外部 variable after changing value is %d\n", num);	


	return 0;
}

 

Compiling both of the programs:
gcc 1.c 2.c

Output:
./a.out

 

In the main function, global value of variable is 0
In the file 2, in finc_1 function, value of 外部 variable is 0
In the file 2, in finc_1 function, value of 外部 variable after changing value is 20

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章

 

分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

<wbr id="oweKHwx"></wbr>

  • <hgroup id="o9rN8MO"><hgroup id="vnxbJ1O" class="vgyvzel"></hgroup></hgroup>

  • <rp id="gY4zSrJ"><mark id="RfqLV8n"></mark></rp>



    <source id="gCGlF6M" class="gIbmrr6"><strong class="WKzUtcr"></strong></source>

      <xmp class="vFcxFks"><fieldset class="TfHf3yG"><sup id="WlW9rQk" class="WonfBEd"><caption class="W3vG42I"></caption></sup></fieldset>