--
C Storage Classes
Storage classes define the storage location, lifetime, and scope of variables/functions in a C program.
These specifiers are placed before the type they modify.
The following storage classes are available in a C program:
- auto
- register
- static
- extern
auto Storage Class
The auto storage class is the default storage class for all local variables.
Variables defined inside a function default to the auto storage class, which means they are created when the function begins 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 functions, meaning auto can only modify local variables.
register Storage Class
The register storage class is used to define local variables that are stored in a register rather than in RAM. This means the variable has a maximum size equal to the register size (usually one word) and cannot have the unary '&' operator applied to it (because it has no memory location).
The register storage class defines storage in registers, so the variable can be accessed faster, but you cannot directly get its address because it is not stored in RAM. Using the register storage class on variables that need frequent access can improve program execution speed.
{
register int miles;
}
Registers should only be used for variables that require quick access, 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 the variable may be stored in a register, depending on hardware and implementation constraints.
static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program, rather than creating and destroying it each time it enters and leaves scope. Therefore, making local variables static allows them to maintain their values between function calls.
The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of that variable 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 those methods appear in the same file as the static variable or method.
Static variables are initialized only once in the program. Even if the function is called multiple times, the value of the variable is not reset.
The following example demonstrates the application of static to both global and local variables:
Example
#include <stdio.h>
/* function declaration */
void func1(void);
static int count = 10; /* global variable - static is default */
int main()
{
while (count--)
{
func1();
}
return 0;
}
void func1(void)
{
/* 'thingy' is a local variable of 'func1' - initialized only once
* every time 'func1' is called, the value of 'thingy' will not be reset.
*/
static int thingy = 5;
thingy++;
printf(" thingy is %d, count is %dn", thingy, count);
}
In the example, count as a global variable can be used inside functions. After thingy is modified with static, it is not reset each time the function is called.
You may not fully understand this example right now, as I have already used functions and global variables, which have not been covered yet. It's okay if you don't fully understand now; we will explain in detail in later chapters. When the above code is compiled and executed, it produces the following result:
thingy is 6, count is 9
thingy is 7, count is 8
thingy is 8, count is 7
thingy is 9, count is 6
thingy is 10, count is 5
thingy is 11, count is 4
thingy is 12, count is 3
thingy is 13, count is 2
thingy is 14, count is 1
thingy is 15, count is 0
extern Storage Class
The extern storage class is used to define a global variable or function that is declared in another file. When the extern keyword is used, no storage is allocated for the variable; it simply indicates to the compiler that the variable is defined in another file.
The extern storage class is used to provide a reference to a global variable that is 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 have defined a global variable or function that can be used in other files, you can use extern in those other files to get a reference to the defined variable or function. To put it another way, extern is used to declare a global variable or function in another file.
The extern modifier is most commonly used when two or more files share the same global variable or function, as shown below:
First file: main.c
Example
#include <stdio.h>
int count;
extern void write_extern();
int main()
{
count = 5;
write_extern();
}
Second file: support.c
Example
#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %dn", count);
}
Here, the extern keyword in the second file is used to declare the count that has already been defined in the first file, main.c. Now, compile both files as follows:
$ gcc main.c support.c
This will produce the a.out executable. When the program is executed, it produces the following result:
count is 5
Notes
auto is the default storage class for local variables, restricting variables to be used only within functions;
register represents register variables, not used in memory;
static is the default storage class for global variables, indicating that the variable is visible throughout the program's lifetime;
extern represents a global variable, meaning it is visible to all files in the program, similar to the public keyword in Java;
Differences Between Global Variables, Local Variables, Static Global Variables, and Static Local Variables in C
From the perspective of scope:
1. Global variables have global scope. A global variable only needs to be defined in one source file, and then it can be used in all source files. Of course, other source files that do not contain the definition of the global variable need to use the extern keyword to re-declare it.
2. Static local variables have local scope. They are initialized only once and exist from the first initialization until the program ends. The difference between them and global variables is that global variables are visible to all functions, while static local variables are always visible only within the function body in which they are defined.
3. Local variables also have only local scope. They are automatic objects (auto). They do not exist throughout the program's execution; they only exist during the function's execution. After a function call completes, the variable is destroyed and the memory it occupied is reclaimed.
4. Static global variables also have global scope. The difference between them and global variables is that if the program contains multiple files, they only operate within the file where they are defined and cannot operate in other files. That is, variables modified by the static keyword have file scope. This way, even if two different source files both define static global variables with the same name, they are different variables.
From the perspective of memory allocation:
1. Global variables, static local variables, and static global variables are all allocated in static storage, while local variables are allocated on the stack.
2. Global variables themselves are static.