There are many different functions and variables in C, and storage classes can help you describe the features of the variables and functions you include in your code. In short, storage classes define the scope (or visibility) and lifetime of a variable or function within a C program. These classes precede the type they modify and further define the code to define where the information in this variable or function is stored in the computer. Every variable in C has a type and a storage class.
Types of Storage Classes in C
There are four types of storage classes in C: auto, extern, static, and register. Each one defines your variables and functions, determining their storage location, scope, and lifetime. Following is a cheatsheet table you can use to remind yourself of the meaning of each type of storage class in C. Then, we’ll take a deep dive into what each one does and when you should use it.
Storage | Scope | |
---|---|---|
Auto | Stack | Garbage |
Extern | Data Segment | Zero |
Static | Data Segment | Zero |
Register | CPU Register | Garbage |
Initial Value | Lifetime | |
---|---|---|
Auto | Within Block | End of Block |
Extern | Multiple Global Files | Til End of Program |
Static | Within Block | Til End of Program |
Register | Within Block | End of Block |
Storage Classes in C: Auto Class
The Auto class is the default storage class for C programs. As a result, it’s uncommon for someone to use the keyword “Auto” when defining a variable or function with an Auto class. Auto variables and functions can only be accessed within the block they are declared in and cannot be accessed outside of that.
The Auto class of variables and functions has a scope definition of “garbage,” meaning the variable’s value is stored without the variable being initialized or assigned. This type of scope typically appears when the variable is declared but not initialized before it is used or when a variable is assigned to a value of another variable that hasn’t been initialized yet.
Auto variables and functions can also be accessed within nested blocks using the parent block/function that declared the variable or function. However, with some finagling, you can also access an auto variable or function outside of its typical scope using pointers and pointing to the exact memory location where the variable or function is stored.
Auto variables and functions are de facto assigned a garbage value whenever they are declared.
Storage Classes in C: Extern Class
The Extern storage class is designed to tell us, the users, and the program that the value of this variable’s definition is in another castle. Simply put, when you want a variable that is defined in a different block, you want to use the Extern or External class.
This is especially useful if you want to change this variable’s value often; since the extern storage class allows you to store the variable’s value in a separate block, the value can also be changed by further blocks. Essentially, the Extern storage class allows you to create global variables initialized with a legal value with which it’s declared to be used elsewhere in the code.
Extern variables can be accessed from anywhere within the code; any function or block can grab an extern value to use. Global variables can also be turned into extern variables by putting the ‘extern’ keyword before declaring the initial value in any block.
Adding the extern keyword to the variable’s declaration essentially tells the program that we are not defining a new variable but accessing a global variable only. Extern variables are typically used for variables that need access by multiple files, such as in a larger program comprising multiple smaller programs.
Storage Classes in C: Static
Static variables and functions are very popular for those writing programs in C. Static variables retain their declared values even after they leave their scope in the program, allowing the program to avoid using additional memory to re-declare the variable or function when it needs to be used.
Static variables and functions are initialized only once and exist until the program is terminated. Thus, a static variable will have the value it was assigned at the last use within its scope. A static variable’s scope is local to the function that defined it, and global static variables can be accessed from anywhere within the program.
Storage Classes in C: Register
Register variables have largely the same features as auto variables. However, when using a register variable, the compiler will try to save the variable’s value to the microprocessor’s register, should one be available. Storing the value in the microprocessor’s register makes it much easier for the program to retrieve the value and improves speeds greatly.
If there’s no free space in the microprocessor’s register, the variable’s value will be saved to memory only. In this case, you might see some speed drops with the program as it tries to retrieve the variable’s value from the computer’s memory.
Usually, the register class is only used for a few variables that will be used by the program often enough to warrant saving them to the microprocessor’s register.
Storage Classes in C: Syntax to Declare a Storage Class
Storage classes can be declared using the following syntax:
storage_class var_data_type var_name;
Please view the following basic example of declaring several variables in the various storage classes. (Thanks to Google Bard AI for doing what I cannot!)
#include <stdio.h>
// Auto storage class
int auto_var = 10;
// Register storage class
int register_var = 20;
// Static storage class
static int static_var = 30;
// External storage class
extern int extern_var;
int main() {
// Assign a value to the extern variable
extern_var = 40;
// Print the values of all four variables
printf("auto_var: %d\n", auto_var);
printf("register_var: %d\n", register_var);
printf("static_var: %d\n", static_var);
printf("extern_var: %d\n", extern_var);
return 0;
}
This code prints the following:
auto_var: 10
register_var: 20
static_var: 30
extern_var: 40
Declaring a storage class for a function uses the same syntax. Please view the following code from Geeks for Geeks to see how you can declare a storage class for a function.
// A C program to demonstrate different storage
// classes
#include <stdio.h>
// declaring the variable which is to be made extern
// an initial value can also be initialized to x
int x;
void autoStorageClass()
{
printf("\nDemonstrating auto class\n\n");
// declaring an auto variable (simply
// writing "int a=32;" works as well)
auto int a = 32;
// printing the auto variable 'a'
printf("Value of the variable 'a'"
" declared as auto: %d\n",
a);
printf("--------------------------------");
}
void registerStorageClass()
{
printf("\nDemonstrating register class\n\n");
// declaring a register variable
register char b = 'G';
// printing the register variable 'b'
printf("Value of the variable 'b'"
" declared as register: %d\n",
b);
printf("--------------------------------");
}
void externStorageClass()
{
printf("\nDemonstrating extern class\n\n");
// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;
// printing the extern variables 'x'
printf("Value of the variable 'x'"
" declared as extern: %d\n",
x);
// value of extern variable x modified
x = 2;
// printing the modified values of
// extern variables 'x'
printf("Modified value of the variable 'x'"
" declared as extern: %d\n",
x);
printf("--------------------------------");
}
void staticStorageClass()
{
int i = 0;
printf("\nDemonstrating static class\n\n");
// using a static variable 'y'
printf("Declaring 'y' as static inside the loop.\n"
"But this declaration will occur only"
" once as 'y' is static.\n"
"If not, then every time the value of 'y' "
"will be the declared value 5"
" as in the case of variable 'p'\n");
printf("\nLoop started:\n");
for (i = 1; i < 5; i++) {
// Declaring the static variable 'y'
static int y = 5;
// Declare a non-static variable 'p'
int p = 10;
// Incrementing the value of y and p by 1
y++;
p++;
// printing value of y at each iteration
printf("\nThe value of 'y', "
"declared as static, in %d "
"iteration is %d\n",
i, y);
// printing value of p at each iteration
printf("The value of non-static variable 'p', "
"in %d iteration is %d\n",
i, p);
}
printf("\nLoop ended:\n");
printf("--------------------------------");
}
int main()
{
printf("A program to demonstrate"
" Storage Classes in C\n\n");
// To demonstrate auto Storage Class
autoStorageClass();
// To demonstrate register Storage Class
registerStorageClass();
// To demonstrate extern Storage Class
externStorageClass();
// To demonstrate static Storage Class
staticStorageClass();
// exiting
printf("\n\nStorage Classes demonstrated");
return 0;
}
This code prints the following:
// This code is improved by RishabhPrabhu
...ster: 71
--------------------------------
Demonstrating extern class
Value of the variable 'x' declared as extern: 0
Modified value of the variable 'x' declared as extern: 2
--------------------------------
Demonstrating static class
Declaring 'y' as static inside the loop.
But this declaration will occur only once as 'y' is static.
If not, then every time the value of 'y' will be the declared value 5 as in the case of variable 'p'
Loop started:
The value of 'y', declared as static, in 1 iteration is 6
The value of non-static variable 'p', in 1 iteration is 11
The value of 'y', declared as static, in 2 iteration is 7
The value of non-static variable 'p', in 2 iteration is 11
The value of 'y', declared as static, in 3 iteration is 8
The value of non-static variable 'p', in 3 iteration is 11
The value of 'y', declared as static, in 4 iteration is 9
The value of non-static variable 'p', in 4 iteration is 11
Loop ended:
--------------------------------
Storage Classes demonstrated
Final Thoughts
So that’s everything you need to know about using your shiny new storage classes in C. Using storage classes is an excellent way to help optimize your code and bring it to the next level. We highly recommend it!
The image featured at the top of this post is ©Roman Samborskyi/Shutterstock.com.