C Storage Classes Auto Extern Static Register Explained

There are four categories of storage classes in the C programming language. These include:

The provided storage classes table illustrates their functionality across various domains like storage location, initial value, visibility, and duration of existence.

Storage Classes Storage Place Default Value Scope Lifetime
Auto RAM Garbage Value Local Within the function in which they are declared.
Extern RAM Zero Global Till the end of the main program Maybe declared anywhere in the program.
Static RAM Zero Local Till the end of the main program. It retains the value between multiple functions calls.
Register Register Garbage Value Local Within the function in which they are declared.

Here, we will discuss these storage classes in C one by one.

Automatic (Auto) Storage Class

The automatic (Auto) storage class is the default storage class for local or block variables.

  • The memory for the automatic variables is allocated automatically at the time of runtime.
  • The visibility and scope of the automatic variables are limited to the block in which they are defined.
  • The automatic variables are initialized to garbage by default.
  • The memory assigned to automatic variables gets freed upon exiting from the block.
  • The keyword used for defining automatic variables is 'auto'.

Automatic Storage Classes Example in C

Let's consider an example to demonstrate the automatic storage classes in C programming.

Example

Example

#include <stdio.h>  

int main()    //main function 

{  

int a; //auto  

char b;  

float c;   

printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c.   

return 0;  

}

Output:

Output

32766  0.000000

Explanation:

In this instance, we define an integer variable that is scoped within the main method. The variable 'a' is automatically initialized by default. Subsequently, we define a character variable and a floating-point variable. Lastly, the program displays the default initial values of the three distinct types of automatically initialized variables.

Automatic Variable Scope and Shadowing in C

Let's consider a scenario to demonstrate the concept of variable scope and shadowing in the C programming language, focusing on the automatic storage class.

Example

Example

#include <stdio.h>  

int main()    //main function

{  

int a = 10,i;   

printf("%d ",++a);  

{  

int a = 20;   

for (i=0;i<3;i++)  

{  

printf("%d ",a); // 20 will be printed 3 times since it is the local value of a  

}  

}  

printf("%d ",a); // 11 will be printed since the scope of a = 20 is ended.   

}

Output:

Output

11 20 20 20 11

Explanation:

In this illustration, we showcase the concept of variable scope and shadowing in C programming. Initially, we define a variable 'a' with an initial value of 10, which is then increased to 11 and displayed. Subsequently, we introduce a nested block where we declare a new local integer variable 'a' with a value of 20. Within this block, a for loop is implemented to iterate as long as the condition i < 3 holds true, with the 'i' variable being post-incremented. This loop outputs the value of the local variable 'a' three times. Upon exiting the nested block, the outer 'a' (with a value of 11) is once again printed.

Static Storage Classes in C

In C programming, the static storage class informs the compiler to store a local variable in memory till the program exists, without the need to create and destroy it each time it comes into and goes out of the scope.

  • The variables defined as static specifier can hold their value between multiple function calls.
  • Static local variables are visible only to the function or the block in which they are defined.
  • The same static variable can be declared many times, but can be assigned only once.
  • The default initial value of the static integral variable is 0; otherwise, null.
  • The visibility of the static global variable is limited to the file in which it is declared.
  • The 'static' keyword is used to define a static variable.

Static Storage Classes Example in C

Let's consider an example to demonstrate the static storage classes in C programming.

Example

Example

#include<stdio.h>  

static char c;  

static int i;  

static float f;   

static char s[100];  

void main ()    //main function

{  

printf("%d %d %f %s", c, i, f); // the initial default value of c, i, and f will be printed.   

}

Output:

Output

0 0 0.000000 (null)

Explanation:

In this instance, we define three static variables of char, int, and float types named c, i, and f respectively. Subsequently, we define an array s of type char with a capacity of 100. Within the main block, we assign the default initial values to c, i, and f.

Static variables in C retain their values across multiple function calls.

Let's consider an example to demonstrate how static variables maintain values between function invocations in the C programming language.

Example

Example

#include<stdio.h>  

void sum()  

{  

static int a = 10;  

static int b = 24;   

printf("%d %d \n",a,b);  

a++;   

b++;  

}  

void main()   //main function

{  

int i;  

for(i = 0; i< 3; i++)  

{  

sum(); // The static variables holds their value between multiple function calls.    

}  

}

Output:

Output

10 24

11 25

12 26

Explanation:

In this instance, we define a sum function with a void return type. Initially, we set up two static local integer variables, 'a' and 'b'. Subsequently, we increment the values of 'a' and 'b' by 1. Within the main function, we declare a local integer variable 'i'. Following this, a for loop is executed thrice, incrementing the value of 'i'. Ultimately, the sum function is invoked to maintain the values of static variables across various function calls.

Register Storage Classes in C

In C programming, the register storage class is mainly used for local variables to store them in the registers instead of storing them in Random Access Memory (RAM). When a variable is stored in registers, it means that the variable will have a maximum size equal to the register size, and cannot have the unary AND operator which is applied to it.

  • The variables defined as the register get allocation in the memory of the CPU registers, which depends upon the size of the memory remaining in the CPU.
  • We cannot dereference the register variables, i.e., we cannot use &operate for the register variable.
  • The access time of the register variables is faster than the automatic variables.
  • The initial default value of the register local variable is 0.
  • The 'register' keyword is used for the variable that should be stored in the CPU register. However, it is the compiler's choice whether or not the variables can be stored in the register.
  • We can store pointers in the register, i.e., a register can store the address of a variable.
  • Static variables cannot be stored in the register because we cannot use more than one storage specifier for the same variable.
  • When we want to access a variable quickly, such as to access counters, use the register storage class.
  • Note: It is not necessarily meant that a variable will be stored in a register only when defining a register. It may or may not be stored in a register, all depends on the hardware and implementation restrictions.

Register Storage Class Example in C

Let's consider a scenario to demonstrate the register storage class in the C programming language.

Example

Example

#include <stdio.h>  

int main()    //main function

{  

register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0.   

printf("%d",a);  

}

Output:

Explanation:

In this instance, we define a local integer variable named a that will be stored in the CPU register, initialized with a default value of 0, and then proceeds to display the value of a.

External (extern) Storage Classes in C

In C programming, the external storage class gives a reference to a global variable, and it is visible to all code files.

  • The external storage class is used to tell the compiler that the variable defined with the keyword 'extern' is declared with an external linkage elsewhere in the program.
  • The variables declared as extern are not allocated any memory. It is only a declaration and is intended to specify that the variable is declared elsewhere in the program.
  • The default initial value of the external integral type is 0; otherwise, null.
  • We can only initialize the extern variable globally, i.e., we cannot initialize the external variable within any block or method.
  • An external variable can be declared many times, but can be initialized only once.
  • If a variable is declared as external, the compiler searches for that variable to be initialized somewhere in the program, which may be extern or static. If it is not, then the compiler will show an error.
  • We use the extern modifier when we want to share two or more files.

External Storage Classes Example in C

Let's consider a scenario to demonstrate the external storage class in the C programming language.

Example

Example

#include <stdio.h>  

int main()   // main function

{  

extern int a;   

printf("%d",a);  

}

Output:

Output

main.c:(.text+0x6): undefined reference to `a'

collect2: error: ld returned 1 exit status

Explanation:

In this instance, we declare an integer local and external variable named a, indicating that it will not be assigned any memory space. Subsequently, the program attempts to display the value stored in a, resulting in an error due to the absence of a reference for variable a.

An external example in C involves utilizing the "extern" keyword with a global variable.

Let's consider an example to demonstrate the use of the extern keyword with a global variable in the C programming language.

Example

Example

#include <stdio.h>  

int a;   

int main()    //main function

{  

extern int a; // variable a is defined globally, the memory will not be allocated to a  

printf("%d",a);  

}

Output:

Explanation:

In this instance, we declare an integer global variable named a. Within the main function, a local external integer variable named a is declared without memory allocation. Subsequently, the program outputs the value of this variable a. The output will be 0 as the reference of the local variable a is associated with the global variable a, which defaults to the value of an external variable.

The extern keyword in C is utilized to declare a global variable that is defined later in the program.

Let's consider an instance to demonstrate the utilization of the extern keyword in C for accessing a global variable that is defined subsequently.

Example

Example

#include <stdio.h>  

int main()    //main function

{  

extern int a; // Compiler will search here for a variable a defined and initialized somewhere in the program or not.   

printf("%d",a);  

}  

int a = 20;

Output:

Explanation:

In this instance, we showcase the utilization of the extern keyword in C to reference a global variable that is defined later. Within the main function, we define a local extern integer variable named a. Subsequently, it displays the value of variable a, prompting the compiler to locate a defined and initialized variable a elsewhere in the program.

Characteristics of Storage Classes in C

There are several key characteristics of Storage Classes in C. Some of them are as follows:

  • Visibility: The scope of auto and register variables is the block or method, and cannot be accessed outside. We can access the static variables globally within the file where are declared. The extern variable allows global variables to be shared across different code files, which creates modularity in large programs.
  • Existence: The auto and register variables exist only when the method is executing, and get destroyed once the method execution completes or exits. The static variables retain their values throughout the execution of the program, and it does not matter the number of times the method is invoked. The extern variables exist throughout the whole program execution, which is useful for sharing states.
  • Maintaining Code Modularity and Readily: The appropriate usage of the storage classes improves the modularity and organization of the code. By limiting the scope of variables wherever needed, it becomes easy to debug, scale, and maintain.
  • Implementing faster loops and performance critical code: In order to high-performance computing, the register storage class is better when minimizing the memory access time is crucial. Variables declared with the register keyword are stored in CPU registers, which allows faster access as compared to variables stored in RAM. It helps to make loops and intensive mathematical operations more efficient.
  • Usage of Storage Classes in C

There are several usage of storage classes in C. Some of them are as follows:

  • Use auto storage class implicitly for local variables because it is the default storage class for local variables.
  • If we want to access variables frequently and quickly, use register storage classes. It is mainly for 'for' loop counters, or frequently used variables for performance improvement. It is not recommended to use register storage class for accessing memory addresses, or for large data types.
  • When a variable is required to retain its value between method calls, use the static storage class.
  • If we want to restrict global variables so as to prevent any unwanted modifications, use the static storage class to limit their scope.
  • The extern storage class is used to access global variables in multiple files.
  • Minimize the usage of the global variables by using function parameters or static local variables, and not by declaring all variables as extern variables.

Input Required

This code uses input(). Please provide values below: