C Storage Classes
Storage class definitions determine the storage location, lifetime, and scope of variables/functions in C programs.
These specifiers are placed before the type they modify.
The following lists the storage classes available in C programs:
- auto
- register
- static
- extern
auto storage class
autoThe storage class is the default storage class for all local variables.
Variables defined in a function default to the auto storage class, meaning they are created when the function starts and destroyed when the function ends.
{
int mount;
auto int month;
}
The example above defines two variables with the same storage class. auto can only be used inside a function, that is, auto can only modify local variables.
register storage class
registerThe storage class is used to define local variables stored in registers rather than in RAM. This means the maximum size of the variable is equal to the register size (usually one word), and the unary '&' operator cannot be applied to it (because it has no memory location).
The register storage class defines storage in registers, so access to the variable is faster, but it cannot be directly addressed because it is not stored in RAM. Using the register storage class on variables that need frequent access can improve the program's running speed.
{
register int miles;
}
Registers are only used for variables that require fast access, such as counters. It should also be noted that defining 'register' does not mean the variable will be stored in a register; it means the variable may be stored in a register, depending on hardware and implementation limitations.
static storage class
staticThe storage class instructs the compiler to keep a local variable in existence throughout the program's lifetime, without needing to create and destroy it each time it enters and leaves its scope. Therefore, using static to modify a local variable can preserve the local variable's value between function calls.
The static modifier can also be applied to global variables. When static modifies a global variable, it restricts the variable's scope to the file in which it is declared.
A static variable or method declared globally can be called by any function or method, as long as these methods appear in the same file as the static variable or method.
A static variable is initialized only once in the program. Even if the function is called multiple times, the value of the variable will not be reset.
The following example demonstrates the application of static modifying global variables and local variables:
Example
In the example, count is a global variable that can be used inside functions; after thingy is modified with static, it will not be reset on each call.
You may not understand this example yet, because I have already used functions and global variables, two concepts that have not been explained so far. Even if you cannot fully understand it now, it doesn't matter; we will explain in detail in later chapters. When the above code is compiled and executed, it produces the following results:
thingy 为 6 , count 为 9 thingy 为 7 , count 为 8 thingy 为 8 , count 为 7 thingy 为 9 , count 为 6 thingy 为 10 , count 为 5 thingy 为 11 , count 为 4 thingy 为 12 , count 为 3 thingy 为 13 , count 为 2 thingy 为 14 , count 为 1 thingy 为 15 , count 为 0
extern storage class
externThe storage class is used to define global variables or functions declared in other files. When the extern keyword is used, no storage space is allocated for the variable; it merely instructs the compiler that the variable is defined in another file.
externThe storage class is used to provide a reference to a global variable that is visible to all program files. When you useextern, for a variable that cannot be initialized, it points the variable name 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 useexternin other files to obtain a reference to the defined variable or function. It can be understood thatexternis used to declare a global variable or function in another file.
The extern modifier is usually used when two or more files share the same global variable or function, as shown below:
First file: main.c
Example
Second file: support.c
Example
Here, in the second file,externThe keyword is used to declare the variable already defined in the first file main.ccount. Now, compile these two files as follows:
$ gcc main.c support.c
This will producea.outexecutable program, when the program is executed, it produces the following results:
count is 5other extensions