What is Storage Classes in C Language?

Storage class C allocates a storage area for variables that are kept in memory. They are stored in the system RAM. Determine the scope of variables separately from storage space. Variables in C programs are mainly stored in the physical location of device memory and random memory, which is the CPU register. There are four types of storage classes: automatic, external, static, and log. You can specify a storage specifier with a variable.

Types of Storage Classes:

There are four types of storage classes are as follows:

1. Automatic Storage Class:

All variables declared within a function or block are stored in auto-specifiers by default, even if they are not explicitly defined. Scope or Visibility of a variable in an automatic storage class is local to the block or function in which it is defined. When you exit a function or block, the variable is destroyed.

For example:
Here Code:
#include <stdio.h>
int sum(int n1, int n2){
  auto int s;//declaration of auto(local) variable
  s = n1+n2;
  return s;
}
int main(){
  int i = 2, j = 3, k;
  k = sum(i, j);
  printf("sum is : %d\n", k);
  return 0;
}

2. Register Storage Class:

Variables stored in the record storage class are also locally scoped. That is, it can only be accessed or viewed in the declared block. This storage is similar to automatic, with the main difference that automatic variables are stored in memory, while registry variables are stored in CPU registers. This is done if you access the variables frequently. These can be used faster. Very few variables are stored using registry specifiers. If there is no space in the registry, it will only be stored in memory. No initial value is assigned to the registry variable. Also, the & (address of) operator cannot be used in registry variables. For example, variables that are used for counters or similar usage types are stored using registry specifiers.

For example:
Here Code:
#include <stdio.h>
int main()
{
    register int n = 20;
    int *ptr;
    ptr = &n;
    printf("address of n : %u", ptr);
    return 0;
}

3. Static Storage Class:

Variables, whether global or local, are stored in static storage classes using static specifiers when the variable must be declared once and the value must be retained. When a variable is declared static, the value is saved or persisted between function calls. Persistent storage is created and declared only once. When a local variable is declared static, persistent storage is created and the value is retained each time it is used. Also, depending on the scope of regular local variables, static local variables only appear in the function or block in which they are defined. As with static locales, when a global variable is declared static, persistent storage is created and declared only once. However, even globally, these variables are only visible in the file in which they are defined.

For example:

Where g and i are static variables, “g” is a global variable and “i” is a local variable. If you didn’t write static before the declaration of ‘i’, then every time the function ‘fn ()’ is called, ‘i’ ends the function ‘fn ()’ with an initial value of 0 and destroys it as well. It would have been done.

Here Code:
#include <stdio.h>
 static int g = 5;
 void fn(){
 	static int i = 0;
 	printf("g = %d\t", g--);
 	printf("i = %d\n",i++);
 }
int main(){
  while(g >= 2)
  fn();
  return 0;
}

4. Extern Storage Class:

A variable declared as external indicates that the variable is defined elsewhere in another program. These external variables are used when you want to use a variable or function defined in a program in another file. Variables with external specifiers are stored in the external storage class. If extern is declared to be a program, the variable specifies an external binding and will not be redefined or initialized. Only one is allocated to storage and is only initialized once. If the external variable is reinitialized with another value in the external program, you will get a “variable redefine” error.

For example:
Here Code:
main.c:

#include <stdio.h>
extern i;
main() {
   printf("value of the external integer is = %d\n", i);
   return 0;}
original.c:
#include <stdio.h>
i=48;

Conclusion:

Storage class C is used to represent additional information about variables. The storage class represents the scope and lifespan of the variable. It also shows who can access the variable from where. Auto, extern, register, and static are four different storage classes for C programs. The C storage class specifiers are used to define variables, functions, and parameters. auto is used for local variables defined within a block or function. Registers are used to store variables in CPU registers instead of memory locations for quick access. Each has a use case within the C program.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *