存储类用于了解变量的范围和生存期。有两种类型的存储类别。
- 本地
- 全球
以下是不同的存储类说明符:
- 汽车
- 寄存器
- 静态的
- 外部
在研究存储类之前,我们将讨论变量及其在不同块中的行为。 C中的一个块是写在“ {…}”花括号内的语句集。
以下是C语言中可用的不同类型的块:
- 功能块。
- 文件块。
- 全局块。
- 嵌套块。
1.自动变量:
在函数内部声明的任何变量将属于自动存储类。“auto” is the keyword used to declare a variable as a 汽车matic storage class.
- 自动变量存储在堆栈段中。
- 它们的范围在定义它们的块中。
- 它们的寿命直到功能执行为止。
- 如果未定义,它们的默认值将是垃圾。
例:
汽车 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.
- Register variables are stores in the computer 寄存器 if available.
- 它们的范围在定义它们的块中。
- 它们的寿命一直到该块执行为止。
- 如果未定义,它们的默认值将是垃圾。
例:
寄存器 int i = 10;
3.静态说明符:
“static” is the keyword used to declare a variable as a 静态的 storage class.
- 静态的 variables are stores in the data segment.
- 它们的寿命一直到程序执行为止。
- 如果未定义,则其默认值为零。
- 如果将局部变量声明为静态变量,则该变量将在函数调用之间保持其值。
- 如果全局变量声明为静态变量,则该变量范围就是文件的范围。不能从其他文件访问该变量。
例:
静态的 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.
- 外部 variables are stores in the data segment.
- 它们的寿命一直到程序执行为止。
- 如果未定义,则其默认值为零。
- 范围在已导入变量的文件中。
例:
外部 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+章 |