Storage Classes #
In C programming, a variable is a memory location used to store and retrieve data.
Early on in this module we were introduced to variables. We found that every variable has five attributes:
- type
- identifier
- value
- size
- memory location
.
We also explored the syntax for declaring a variable and why it is important to declare our variables:
- It tells the compiler how much space to allocate for the variables storage in memory.
- It tells the compiler what type of operations are legal on the variable.
- It tells the compiler what rules of scope to apply to the variable.
- This relates to the GLOBAL vs LOCAL variables which we encountered previously.
A LOCAL variable is one that is declared inside a function. The LOCAL variable will only exist inside that function.
A GLOBAL variable is one that is declared outside of functions. The GLOBAL variable will exist throughout the entire program.
.
Storage Class & Scope #
EVERY variable has a storage class and a scope. The storage class of a variable determines:
- the part of memory where the storage is allocated.
- how long the storage allocation will exist.
- the scope (visibility) of the variable.
There are four primary storage classes for variables in c programming:
- auto
- register
- static
- extern
auto Storage Class #
The majority of the variables we have used to date have all been os storage class auto. This is the default storage class for ALL variables declared inside a function. Therefore they are LOCAL variables. They are allocated memory when the function in which they are decalred is called. The memory is dellocated once the function completes. The characteristics of the auto storage class are:
- SCOPE: LOCAL (only visible within the function they are delcared.)
- DEFAULT VALUE: ANYTHING
- MEMORY LOCATION: RAM
- LIFETIME: Until the end of its scope
|
|
.
register Storage Class #
The register storage class is similar to the auto one in that their scope is local to the function in which they are declared and their default value can be anything. The main difference between the register and the auto storage class is the location in memeory in which they are allocated.
- auto variables are allocated storage in the memory (RAM) of the computer.
- register variables are allocated memory in the CPU in a storage cell known as a register.
The reason to use the register storage class is if the variable is repeatedly used or if their access is time critical. Data stored in the regsiters of a CPU can be accessed much faster than data stored in RAM so therefore a variable may be declared with the register storage in order to access it much quicker than if it was store in RAM (auto).
Using the register keyword DOES NOT guarantee that the variable will be allocated memory in the CPU. The compiler will check to see if there is a register available. If there is one available it will assign this register to the variable. However, if there are no registers free, then the variable will be stored in RAM as it were an auto variable.
The characteristics of the register storage class are:
- SCOPE: LOCAL (only visible within the function they are delcared.)
- DEFAULT VALUE: ANYTHING
- MEMORY LOCATION: Register in CPU (priority) or RAM
- LIFETIME: Until the end of its scope
|
|
.
static Storage Class #
The static storage class is used in conjunction with variables and functions. Declaring a variable in a function with the static keyword causes the variable to keep its value between function calls. Remember, a variable local to a function will lose its value after a function ends. By using the static keyword, the variable will now keep its value after the function ends. So the next time the function is called, the variable will still have the value from the last the time the function executed. Typcical use cases for the static storage class are:
- With local variables inside a function so that they do not lose their value between function calls.
- With global variables, so that they are restricted to the file they are defined in. A large c program might have many files. Using the static restricts the scope of a global variable to the file in which it is defined.
- With functions, so that they are not visible outside of the C file in which they are defined.
The characteristics of the register storage class are:
- SCOPE: LOCAL
- DEFAULT VALUE: 0
- MEMORY LOCATION: RAM
- LIFETIME: Until the end of the program.
|
|
.
extern Storage Class #
The extern keyword informs the compiler that the variable we are concerned with is defined elsewhere in the program. This is often used where a program has multiple files that need to be compiled. A variable could be declared in one file, and to use it in another file, we use the extern keyword. This informs the compiler that we want to use a variable but it is defined in a different file. The compiler cna locate it (as long as it has access to the file) and the variable can be used in multiple files. This is an extension to the global varible concept.
As an example lets consider the code below in a file called file1.c. This is a file that declares a global variable named globalVar.
|
|
Now consider the code below which is located in a file called file2.c. This has the function fun() that is trying to access the global variable defined in the first file. Because the extern keyword was used for declaration of the variable in the second file (file2.c), the compiler points to the variable that was declared in the first file (file1.c). This results in NO MEMORY being allocated to the variable from the second file but the code in the secind file can still access the variable in the first. file.
|
|
The characteristics of the extern storage class are:
- SCOPE: GLOBAL
- DEFAULT VALUE: 0
- MEMORY LOCATION: RAM
- LIFETIME: Until the end of the program.