Join our subscribers list to get the latest news, updates and special offers directly in your inbox
Overview
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.
In the C language, we use four distinct types of storage classes:
// 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; }
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
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 :
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 :
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 :
EmbeddedWala
EmbeddedWala Jun 14, 2023 0 21.6K
EmbeddedWala Apr 27, 2023 0 21.1K
EmbeddedWala Apr 26, 2023 0 18.5K
EmbeddedWala Aug 30, 2022 0 16.5K
EmbeddedWala Apr 27, 2023 0 16.5K
EmbeddedWala Jun 19, 2022 0 4.8K
This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies Find out more here