C C++

What are the different types of storage classes in C/C++ programming7 min read

In C programming, there are four storage classes:

  1. 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.
  2. 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.
  3. 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.
  4. 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’ autoregister 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:

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:

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:

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.

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:

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:

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:

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:

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:

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:

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.

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.

Leave a Comment