In C programming, there are four storage classes:
- Automatic storage class: Variables declared within a function or block are automatically allocated memory from the stack and are known as automatic variables. They are also called local variables.
- Register storage class: Variables declared with the “register” keyword are stored in CPU registers, rather than memory. This can improve performance, but the number of registers is limited, so not all variables can be stored in registers.
- Static storage class: Variables declared with the “static” keyword are stored in memory, but retain their value between function calls. They are also called local static variables.
- External storage class: Variables declared outside of any function or block are stored in memory, and are known as global variables. They are also called external variables.
Note that in C++ and some other programming languages, there are different types of storage classes or storage duration that can be used for different purposes.
The storage classes’ auto, register and static can be applied to local variables and the storage classes’ extern and static can be applied to global variables.
auto
In C++ programming, the “auto” keyword is used as a storage class specifier for variables that are automatically deduced by the compiler. The type of the variable is automatically deduced from the type of the initializer expression. The “auto” keyword was introduced in C++11 as a shorthand for declaring variables with the type deduced by the compiler.
For example:
1 2 3 4 |
auto x = 5; // x is of type int auto y = "hello"; // y is of type char* |
In this way, the “auto” keyword helps to reduce the amount of explicit type declarations, making the code more readable and less error-prone.
It is important to keep in mind that the “auto” keyword is not the same as the automatic storage class in C. The “auto” keyword deduces the type of the variable from the initializer expression and the automatic storage class refers to the memory management of the variables.
register
In C and C++ programming, the “register” keyword is used as a storage class specifier for variables that are stored in CPU registers, rather than memory. The purpose of using the “register” keyword is to improve the performance of the program by reducing the number of memory accesses. When a variable is stored in a register, it can be accessed faster than a variable stored in memory.
For example:
1 2 3 |
register int x; |
It is important to note that the use of the “register” keyword is only a suggestion to the compiler, and it is not guaranteed that the variable will actually be stored in a register. The compiler may ignore the request if there are not enough registers available or if it determines that storing the variable in memory would be more efficient.
Additionally, because the number of registers is limited, not all variables can be stored in registers, so it’s important not to overuse the keyword “register”, it will not necessarily improve the performance of the program.
Also, variables that are declared with the “register” keyword have the same lifetime and scope as variables declared with the “auto” keyword and they cannot have their address taken.
extern
In C and C++ programming, the “extern” keyword is used to indicate that a variable is defined in another source file and should be linked to the current source file at compilation time. This allows variables to be shared across multiple source files.
When a variable is declared with the “extern” keyword, it tells the compiler that the variable is defined in another source file, and the compiler should use the definition from that file rather than creating a new one. The variable can be used just like a normal variable, but its memory is allocated in a different source file.
For example:
1 2 3 4 5 6 7 |
// file1.c int x; // file2.c extern int x; |
In this example, the variable x is defined in file1.c and is declared as extern in file2.c. This allows the variable to be used in both files, without creating a duplicate variable.
It’s important to note that the “extern” keyword can also be used in function declarations to indicate that the function is defined in another source file.
1 2 3 |
extern int func(); |
This tells the compiler that the function “func” is defined in another source file, and the compiler should use the definition from that file rather than generating a new one.
It’s also worth noting that global variables are implicitly extern, meaning that they can be accessed from other source files without explicitly declaring them as extern.
static
In C and C++ programming, the “static” keyword can be used as a storage class specifier for variables and functions. The use of the “static” keyword changes the scope and lifetime of the variable or function.
When used for variables, the “static” keyword tells the compiler to keep the variable in memory for the entire lifetime of the program, rather than allocating and deallocating memory for the variable each time it goes in and out of scope. This means that the value of a static variable is retained between function calls. A static variable is only visible within the file it is defined in.
For example:
1 2 3 |
static int x = 0; |
When used for functions, the “static” keyword tells the compiler that the function can only be called within the file it is defined in. This makes the function “private” to the file, and it cannot be called from outside of the file.
For example:
1 2 3 |
static void func() { ... } |
It’s important to note that the “static” keyword can also be used in global variable and function declarations, to indicate that the variable or function has internal linkage, which means that the variable or function can only be accessed within the file it is defined in, even if it has global scope.
In C++, the “static” keyword is also used to indicate that a class member variable or function is shared among all objects of the class, rather than being unique to each object, this is known as a static member.
Examples
Here are some examples of how the “auto”, “register”, “extern”, and “static” keywords can be used in C and C++ programming:
“auto” keyword:
1 2 3 4 5 6 |
void func() { auto x = 5; // x is of type int auto y = "hello"; // y is of type char* } |
In this example, the variables “x” and “y” are declared as “auto” inside the “func” function. The type of the variable is automatically deduced by the compiler based on the initializer expression.
“register” keyword:
1 2 3 4 5 6 7 8 9 |
int main() { register int x = 0; for (register int i = 0; i < 10; i++) { x += i; } return x; } |
In this example, the variables “x” and “i” are declared as “register” inside the “main” function. This is a hint to the compiler that these variables should be stored in CPU registers for faster access.
“extern” keyword:
1 2 3 4 5 6 7 8 9 10 11 |
// file1.c int x = 0; // file2.c extern int x; int main() { x = 5; return 0; } |
In this example, the variable “x” is defined in file1.c and declared as “extern” in file2.c. This allows the variable to be used in both files without creating a duplicate variable.
“static” keyword:
1 2 3 4 5 6 7 |
void func() { static int x = 0; // x is retained between function calls x++; printf("x = %d\n", x); } |
In this example, the variable “x” is declared as “static” inside the “func” function. This tells the compiler to keep the variable in memory for the entire lifetime of the program, rather than allocating and deallocating memory for the variable each time the function is called.
1 2 3 4 5 |
class MyClass { static int x; // x is shared among all objects of the class }; |
In this example, the variable “x” is declared as “static” inside the class “MyClass”. This tells the compiler that the variable is shared among all objects of the class, rather than being unique to each object.
It’s worth noting that the “static” keyword can also be used in global variable and function declarations, to indicate that the variable or function has internal linkage, which means that the variable or function can only be accessed within the file it is defined in, even if it has global scope.