Understanding Storage Classes in C: A Beginner’s Guide
embeddedwala June 28, 2023 27 views 01. Introduction
In C programming language, storage class is a vital concept that influences the lifetime, scope, and visibility of variables. C allows declaring variables with various storage classes, which determine their memory storage, persistence, and accessibility. Acquiring knowledge about storage classes is crucial for creating efficient and successful C programs as it enables programmers to manage memory allocation, optimize code, and maintain data integrity. This blog post will delve into the different storage classes in C, their attributes, and how to utilize them efficiently.
2. Storage Classes Type
In the C language, we use four distinct types of storage classes:
- Automatic Storage Class
- External Storage Class
- Static Storage Class
- Register Storage Class
// extern_test.c
#include <stdint.h> uint8_t extern_var = 8;
// main.c
#include <stdio.h>
#include <stdint.h> extern uint8_t extern_var; uint8_t update_static_var() { /* Static Keyword is used to store */ static uint8_t static_var = 0; static_var++; return static_var; } uint8_t update_auto_var() { /* No Default Keyword means auto type */ uint8_t auto_var = 0; auto_var++; return auto_var; } int main() { /* Register Keyword is used to store */ register uint8_t register_var = 5; printf("Register Var Value = %d\n", register_var); printf("Static Var Update = %d\n", update_static_var()); printf("Static Var Update = %d\n", update_static_var()); printf("Auto Var Update = %d\n", update_auto_var()); printf("Auto Var Update = %d\n", update_auto_var()); printf("Extern Var Value = %d\n", extern_var); return 0; }
3. Automatic Storage Class
The automatic storage class in C is utilized for variables declared within a block or function. Upon entering the block or function, variables declared with the automatic storage class are created automatically, and they are destroyed automatically upon exiting the block or function. If a storage class is not specified for a variable within a function, the automatic storage class is considered to be the default storage class. Variables with the automatic storage class are allocated memory on the stack, and their values are not retained between function calls.
How automatic storage class variables are initialized and scope rules
- Variables with automatic storage class in C are initialized to garbage values if no initial value is assigned to them.
- If an initial value is assigned to an automatic variable, it will be initialized to that value.
- The scope of a variable declared with automatic storage class is limited to the block or function in which it is declared.
- Automatic variables are only accessible within the block or function where they are declared, and are destroyed automatically when the block or function is exited.
- The values of automatic variables are not retained between function calls and will be re-initialized with a garbage value when the function is called again.
- It is important to ensure that automatic variables are properly initialized within the function before they are used to avoid errors caused by uninitialized values.
4. External Storage Class
The external storage class in C programming enables a variable to be declared such that it can be shared and accessed between multiple source files. When declared with the external storage class, variables are stored in global memory and can be accessed by all functions within the same program. This is especially useful when a large variable or an array needs to be shared among multiple functions without having to pass it as a function argument. These variables can be initialized with an initial value or left uninitialized to be initialized later. To declare a variable with the external storage class, the “extern” keyword is used.
Automatic storage class variables are initialized and scope rules :
- External storage class variables can be initialized with an initial value or left uninitialized to be initialized later.
- To declare an external variable, the “extern” keyword is only necessary in one source file. In other source files that need to access the variable, the variable must be declared again with the “extern” keyword but without an initializer.
- The scope of an external variable extends to all source files in the same program, making it accessible from any function within any source file.
- The actual storage for the external variable is allocated in only one source file. Hence, the variable must be defined (allocated storage) in one source file and declared (referenced) in any other source file that needs to access it.
- It is essential to declare external variables in a header file, which can be included in all source files that need to access the variable. This ensures consistency and prevents errors related to multiple declarations of the same variable.
5. Static storage class
The static storage class in C programming is a useful feature that allows variables to retain their value even after the function or block in which they are declared has completed execution. Static variables are declared using the “static” Keyword. These variables are stored in global memory and are initialized only once, at the time of program execution.
In addition to retaining values between function calls, the static storage class is also used to control the scope of a variable, limiting it to the function or block in which it is declared. By using the static storage class, global variables can be created that are only accessible within the file in which they are declared. This helps prevent naming conflicts with variables in other files, making code more modular and organized.
Overall, the static storage class is a powerful tool for C programmers to control the scope and lifetime of variables. By using the static storage class, programmers can create efficient and well-organized code that is easier to maintain and debug.
Static storage class variables are initialized and scope rules :
- Static storage class variables are initialized only once, at the time of program execution.
- If a static variable is declared with an initial value, it is initialized to that value at the time of program execution. If it is not initialized, it is initialized to zero by default.
- The scope of a static variable depends on where it is declared. If it is declared within a function or block, it is only accessible within that function or block. If it is declared outside any function or block, it is accessible to all functions in the same file.
- If a static variable is declared within a function or block, its value is retained between function or block calls. This means that the variable retains its value even after the function or block has completed execution.
- If a static variable is declared outside of any function or block, its value is retained throughout the entire program execution.
- It is important to note that the actual storage for a static variable is allocated only once, at the time of program execution. This means that the variable retains its value between function or block calls because it is stored in the global memory.
6. Register Storage Class
The register storage class in C programming is used to define a variable that is stored in a CPU register instead of in memory. Register variables are declared using the “register” keyword and are typically used for frequently accessed variables that require fast access and manipulation. However, it is important to note that the use of the register keyword is merely a suggestion to the compiler and it is up to the compiler to decide whether or not to store the variable in a register or in memory. Register variables are also typically limited in size and cannot be directly referenced like other variables.
In modern programming, the use of the register storage class has been largely deprecated because most compilers are able to optimize variable storage and access on their own, without requiring explicit hints from the programmer. As a result, the use of register variables is typically unnecessary and can even sometimes result in decreased performance due to compiler optimizations.
Register storage class variables are initialized and scope rules :
- Register variables are automatically initialized to 0 or their default value.
- The scope of register variables is limited to the block in which they are defined. They cannot be accessed outside the block in which they are declared.
- Register variables cannot have their address taken since they are stored in the CPU register, and not in memory. Therefore, the “&” operator cannot be used on register variables.
- The use of register storage class is largely deprecated in modern programming because most compilers can optimize variable storage and access on their own, without the need for explicit hints from the programmer. However, there may be cases where the use of register variables can still provide performance benefits.