Storage Classes in C

Storage classes provide information about a variable’s location and visibility. They decide the portion of the program within which the variables are recognized.

There are broadly four storage classes in C:
auto, external, register and static.

Auto: 

In the language C auto is a keyword for specifying a storage duration. When you create an auto variable it has an “automatic storage duration”. We call these objects “local variables”. Auto variables can be only accessed within the block/function they have been declared and not outside them. In C, all variables in functions are local by default. That’s why the keyword auto is hardly ever used. They are assigned garbage value by default whenever they are declared.
Example:

void some_function() {
  auto int count = 0;
  int count2 = 0; //auto by default
}

The two variables “count” and “count2” have automatic storage duration. In other words, they will exist until the program execution reaches the closing bracket of the block where they were defined. In this case, they will exist until we reach the end of the function. While we are on the subject, let’s take another look at the “block lifetime” variable:

int main() {
    auto int number = 5; {
      auto int number = 20;
      printf("inner number: %d", number);
    }
    printf("
      ");
      printf("outer number: %d", number);

      return 0;
    }

This code works and the result is:
inner number: 20
outer number: 5

Extern: 

Extern storage class simply tells us that the variable is defined elsewhere and not within the same block/file where it is used. Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well. So an extern variable is nothing but a global variable initialized with a legal value where it is declared in order to be used elsewhere. It can be accessed within any function/block. Also, a normal global variable can be made extern as well by placing the ‘extern’ keyword before its declaration/definition in any function/block. This basically signifies that we are not initializing a new variable but instead we are using/accessing the global variable only. The main purpose of using extern variables is that they can be accessed between two different files which are part of a large program.
Few examples:extern int var; int main(void) { return 0; } 

Analysis: This program is compiled successfully. Here var is declared only. Notice var is never used so no problems. extern int var; int main(void) { var = 10; return 0; } 

Analysis: This program throws error in compilation. Because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all. extern int var = 0; int main(void) { var = 10; return 0; }

If an extern variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.

#include "somefile.h"

extern int
var;
int main(void) {
  var = 10;
  return 0;
}

Analysis: Supposing that somefile.h has the definition of var. This program will be compiled successfully.

Register:

 This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available, these are then stored in the memory only. Usually few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program. An important and interesting point to be noted here is that we cannot obtain the address of a register variable using pointers.

To specify the storage class for a variable, the following syntax is to be followed:register char b = ‘G’;

Static: 

This storage class is used to declare static variables which are popularly used while writing programs in C language. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve the value of their last use in their scope. So we can say that they are initialized only once at compile time and exist till the termination of the program. Thus, no new memory is allocated because they are not re-declared. Their scope is local to the function to which they were defined. Inside a function it makes the variable to retain its value between multiple function calls Global static variables can be accessed anywhere in the program. By default, they are assigned the value 0 by the compiler.

int fun() {
  static int count = 0;
  count++;
  return count;
}

int main() {
  printf("%d ", fun());
  printf("%d ", fun());
  return 0;
}

Output:

1 2

Next post Enumeration C++

One thought on “Storage Classes in C

Leave a Reply

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