C storage class
Storage classes define the scope (visibility) and life cycle of variables/functions in a C program. These specifiers are placed before the type they modify. The storage classes available in C programs are listed below:
auto
register
static
extern
auto storage class
auto The storage class is the default storage class for all local variables.
{ int mount; auto int month;}
The above example defines two variables with the same storage class. Auto can only be used within functions, that is, auto can only modify local variables.
register Storage Class
register The storage class is used to define local variables that are stored in registers instead of RAM. This means that the maximum size of a variable is equal to the size of the register (usually a word), and the unary '&' operator cannot be applied to it (because it has no memory location).
{ register int miles;}
Registers are only used for variables that need to be accessed quickly, such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register, it means that the variable may be stored in a register, depending on hardware and implementation limitations.
static storage class
static The storage class instructs the compiler to maintain the existence of a local variable during the life cycle of the program without requiring it to enter and leave the function each time. Domains are created and destroyed. Therefore, using static to modify a local variable maintains its value between function calls.
static modifier can also be applied to global variables. When static is used to modify a global variable, the scope of the variable is limited to the file in which it is declared.
In C programming, when static is used on a class data member, it will cause only one copy of the member to be shared by all objects of the class.
#include <stdio.h> /* 函数声明 */void func(void); static int count = 5; /* 全局变量 */ main(){ while(count--) { func(); } return 0;}/* 函数定义 */void func( void ){ static int i = 5; /* 局部静态变量 */ i++; printf("i is %d and count is %d\n", i, count);}
Maybe you can’t understand this example yet, because I have used functions and global variables, and these two concepts have not been explained so far. Even if you don’t fully understand it now, it doesn’t matter. We will explain it in detail in subsequent chapters. When the above code is compiled and executed, it produces the following results:
i is 6 and count is 4i is 7 and count is 3i is 8 and count is 2i is 9 and count is 1i is 10 and count is 0
extern Storage class
extern The storage class is used to provide a reference to a global variable, Global variables are visible to all program files. When you use 'extern', for variables that cannot be initialized, the variable name points to a previously defined storage location.
When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get the defined variable or function citation. It can be understood that extern is used to declare a global variable or function in another file.
The extern modifier is usually used when there are two or more files sharing the same global variable or function, as follows:
First file: main. c
#include <stdio.h> int count ;extern void write_extern(); main(){ count = 5; write_extern();}
Second file: support.c
#include <stdio.h> extern int count; void write_extern(void){ printf("count is %d\n", count);}
Here, the extern keyword in the second file is used Declare the count already defined in the first file main.c. Now, compile these two files as follows:
$gcc main.c support.c
This will produce the a.out executable program. When the program is executed, it will produce the following results:
5