Storage Classes In C Language

Introduction To Storage Classes In ‘C’ Language

The Storage class is the property of any variable. It will define different features of the variable. In the C programming language, a storage class specifies the lifetime and visibil

ity of a variable or function within a program. The storage class of a variable or function determines where it is stored in memory and how it can be accessed by other parts of the program.

Features Of Storage Classes In ‘C’ Language

  1. Memory Location: The location where the variable will be stored.
  2. Default Value: The initialized value of a variable.
  3. Scope: The scope a variable.
  4. Duration: A lifetime of a variable.
  5. Access: Who can access a variable

Types Of Storage Class In ‘C’ Language

There are 4 different main property or features of any variables in ‘C’ language. The table below represents the storage classes in C.

Storage ClassDescription
autoIt is a default storage class.
staticIt is a local variable which is capable of returning a value even when control is transferred to the function call.
externIt is a global variable.
registerIt is a variable which is stored inside a Register.
Types Of Storage Class

Auto Storage Classes In ‘C’ Language

In the C programming language, the auto storage class is the default storage class for variables declared inside a function.. auto variables are local to the function in which they are declared and are automatically created when the function is called and destroyed when the function returns. auto variables are local to the function in which they are declared and are automatically created when the function is called and destroyed when the function returns.

Here is an example of a function that uses the auto storage class:
#include <stdio.h>

int main(void) {
  // Declare a local automatic variable
  int x = 10;

  printf("The value of x is %d\n", x);

  // Modify the value of x
  x = 20;

  printf("The value of x is now %d\n", x);

  return 0;
}

In this example, the variable x is declared as an automatic variable within the main() function. It is created when the function is called and is destroyed when the function returns. The value of x is initially set to 10 and is then modified to 20. The program prints the value of x to the screen both before and after the modification.

Automatic variables are useful for storing temporary values that are needed only within a specific function and do not need to be accessed from other parts of the program. They are also useful for keeping track of the state of a program within a function.

The auto storage class is generally used for variables that are needed only within a particular function and do not need to be accessed from other parts of the program. Using the auto storage class can improve the readability of the code by making it clear that the variable is local to the function.

Register Storage Class In ‘C’ Language

The register storage class is used to declare variables that should be stored in a register instead of in main memory. This can improve the performance of the program by reducing access time to the variable. However, the number of registers is limited, so not all variables can be stored in a register.

In the C programming language, the register storage class is used to declare variables that should be stored in a register instead of in main memory. This can improve the performance of the program by reducing access time to the variable.

Here is an example of a function that uses the register storage class:

#include <stdio.h>

int sum(int n) {
  register int i, sum = 0; // i and sum are register variables
  for (i = 1; i <= n; i++) {
    sum += i;
  }
  return sum;
}

int main() {
  int result = sum(100);
  printf("Sum of 1 to 100: %d\n", result);
  return 0;
}

In this example, the function sum() includes two register variables: i and sum. These variables are stored in registers instead of in main memory, which can improve the performance of the program by reducing access time to the variables. The function sum() calculates the sum of the integers from 1 to n and returns the result.

It is important to note that the use of the register storage class is only a suggestion to the compiler, and the compiler may choose to ignore it and store the variables in main memory if it determines that doing so would be more efficient. The number of registers available on a particular system is also limited, so not all variables can be stored in a register.

The register storage class is generally used for variables that are accessed frequently within a function and do not need to be accessed from other parts of the program. Using the register storage class can improve the performance of the program by reducing access time to the variable, but it is important to use it wisely to avoid using up all of the available registers.

Static Storage Class In ‘C’ Language

In the C programming language, the static storage class is used to declare variables that have a lifetime that extends beyond the function in which they are declared. static variables are initialized only once and retain their value between function calls. static variables are also local to the function in which they are declared, but their visibility is limited to the file in which they are defined.

Here is an example of a function that uses the static storage class:

#include <stdio.h>

void piembsystech() {
  static int x = 0; // x is a static variable
  x++;
  printf("x = %d\n", x);
}

int main() {
  piembsystech(); // prints "x = 1"
  piembsystech(); // prints "x = 2"
  piembsystech(); // prints "x = 3"
  return 0;
}

In this example, the function piembsystech() includes a static variable x. The first time piembsystech() is called, x is initialized to 0 and then incremented by 1. The value of x is then printed to the console. When piembsystech() is called again, x retains its value from the previous call and is incremented by 1 again. This process is repeated each time piembsystech() is called, resulting in the values 1, 2, and 3 being printed to the console.

It is important to note that the static storage class is different from the static keyword used in other contexts, such as when declaring a function as static to limit its visibility to the file in which it is defined.

The static storage class is generally used for variables that are needed only within a particular function and need to retain their value between function calls. Using the static storage class can improve the readability of the code by making it clear that the variable is local to the function and retains its value between calls. It can also help to avoid global variables, which can lead to unintended side effects and make it more difficult to understand the program’s behavior.

Extern Storage Class In C Language

In the C programming language, the extern storage class is used to declare variables or functions that are defined in another source file. The extern keyword is used to declare the variable or function in the current source file, and the actual definition must be provided in another source file that is compiled and linked with the current source file.

Here is an example of how the extern storage class is used in two source files:

File: main.c

#include <stdio.h>

// Declare the variable 'x' as extern
extern int x;

int main() {
  printf("x = %d\n", x);
  return 0;
}

File: x.c

// Define the variable 'x' as an int
int x = 123;

In this example, the main source file main.c includes a declaration of the variable x as extern, which means that x is defined in another source file. The definition of x is provided in the source file x.c, which defines x as an int with a value of 123. When the two source files are compiled and linked together, the extern declaration in main.c refers to the definition of x in x.c, allowing main.c to access the value of x.

The extern storage class is generally used to share variables or functions between multiple source files. It allows multiple source files to access the same data or functions without having to duplicate the definitions in each file. This can help to reduce the size of the program and improve its maintainability.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading