Storage Classes In C

Storage Classes In C

Storage classes in the C programming language play a fundamental role in defining how variables and functions behave within a program. These storage classes provide a mechanism to specify crucial attributes, such as scope, visibility, and lifetime, which have a significant impact on a program’s structure and execution. By understanding storage classes, programmers gain control over how memory is allocated, how variables are accessed, and how functions are utilized, allowing for more efficient and organized code. In this exploration of storage classes in C, we will delve into the different types of storage classes, their characteristics, and their practical applications, providing a comprehensive understanding of their significance in C programming.

1. auto

“auto” is the default storage class for variables declared within a function or block. These variables have local scope, meaning they are accessible only within the block or function in which they are declared. The “auto” keyword is rarely used explicitly because variables without an explicitly specified storage class are automatically considered “auto” by default. They have a limited lifetime, as they are automatically created when the block is entered and destroyed when the block exits. “auto” variables can also be accessed within nested blocks within the parent block or function in which they are declared. This makes them suitable for temporary or short-lived variables used within a specific context.

While “auto” variables are typically limited in scope to the block or function in which they are declared, it’s important to note that they can technically be accessed outside their scope using pointers. If you obtain the memory address of an “auto” variable and store it in a pointer, you can access the variable’s value from other parts of the program.

However, doing so can be risky and is generally discouraged because it can lead to undefined behavior and memory issues if not handled carefully. “Auto” variables are intended to be local in scope, and accessing them via pointers from outside their scope can result in unpredictable and unintended consequences.

2.extern

The description of the “extern” storage class in C is largely accurate. The “extern” keyword is used to indicate that a variable is defined elsewhere, typically in a different file or translation unit, and not within the same block or function where it is used. The main characteristics of “extern” variables are as follows:

  1. Global Access: “extern” variables are global in scope, meaning they can be accessed from any function or block within the same program.
  2. Legal Initialization: While “extern” variables are declared in one file, they are often initialized with a legal value in another file or translation unit. This allows variables to be shared and used across different parts of a program.
  3. No Memory Allocation: “extern” variables do not allocate memory for the variable itself; instead, they refer to an existing variable defined elsewhere. This is important because it avoids redundancy and ensures that there is only one instance of the variable in memory.
  4. Linking Files: One of the primary purposes of using “extern” variables is to enable communication between different source code files in a large program. This allows different parts of the program to share data and variables.

Additionally, the statement correctly mentions that you can declare a global variable as “extern” in a local block or function, indicating that you are not creating a new variable but instead using the global variable. This is a way to access global variables from within a specific context.

Overall, “extern” is a crucial concept for inter-file communication and sharing variables in C programs.

3.static

Static variables have some distinctive properties, and your statement captures many of them. Here’s a breakdown of the key points mentioned:

  1. Preservation of Value: Static variables preserve their values between function calls. They retain the value of their last use within their scope, even after going out of scope. This makes them useful for maintaining state across multiple calls.
  2. Initialization: Static variables are initialized only once, typically with a default value of 0 by the compiler. You can provide an explicit initialization value if needed.
  3. Lifetime: They exist for the entire duration of the program, from the moment they are initialized until the program terminates. No new memory is allocated for them because they are not re-declared.
  4. Scope: Static variables have local scope within the function in which they are defined. This means they are accessible only within that specific function. Global static variables, declared outside any function, can be accessed from anywhere in the program but are limited to that program’s file.

These characteristics make static variables valuable for certain programming scenarios, such as maintaining counters or tracking state across function calls, without the need for global variables.

4. register

Register variables are a type of storage class that is intended for optimization, and your statement highlights the key points associated with them:

  1. Use of Registers: Register variables are similar to auto variables but come with an optimization aspect. The compiler attempts to store these variables in CPU registers if registers are available. This is because CPU registers are faster to access than variables stored in memory.
  2. Falling Back to Memory: If there are no available CPU registers (registers are limited resources), the compiler will fall back to storing these variables in memory.
  3. Performance Improvement: Register variables can improve the running time of a program when used for variables that are accessed frequently because reading from and writing to CPU registers is faster than accessing variables in memory.
  4. Address Inaccessibility: As you correctly mentioned, it’s important to note that you cannot obtain the address of a register variable using pointers. Register variables are not addressable because they are intended to be stored in CPU registers, and CPU registers do not have memory addresses that are accessible to the programmer.

However, it’s worth mentioning that modern optimizing compilers are quite effective at managing variable storage and register allocation. In many cases, explicit use of the “register” keyword may not be necessary, as compilers can automatically optimize variable storage for performance.

Syntax

Syntax of storage class of variable is given below:

storage_class var_data_type var_name;

Example

/ 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; 
} 

Output

A program to demonstrate Storage Classes in C


Demonstrating auto class

Value of the variable 'a' declared as auto: 32
--------------------------------
Demonstrating register class

Value of the variable 'b' declared as register: 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:
--------------------------------

FAQ- Storage Classes In C

Q1. What are the storage classes in C?

Ans. There are four different types of storage classes that we use in the C language:
Automatic Storage Class.
External Storage Class.
Static Storage Class.
Register Storage Class.

Q2. What is storage class in C example?

Ans. The storage classes in the C programming language serve as descriptors for variables and functions, encapsulating key attributes. These attributes primarily encompass scope, visibility, and lifetime, collectively aiding in the monitoring of a variable’s existence during a program’s runtime.

Q3. What is storage class and types?

Ans. Storage classes in C encompass both the scope and the lifespan of a variable. They also define the accessibility of variables and their locations within a program. C provides four main storage classes: auto, register, extern, and static. These storage classes play a fundamental role in determining how variables behave and are accessed in a program.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment