Storage class determines the scope, visibility, memory location, default value and lifetime of a variable or function. A storage class in C is used to describe the following things.

  • Variable’s scope.
  • Location where the variable will be stored.
  • Initialized value of a variable.
  • Lifetime of a variable.
  • Who can access a variable?

There are four types of storage classes in C:-

  1. auto
  2. extern
  3. static
  4. register
Storage ClassesMemory LocationDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of program
staticRAMZeroLocalTill the end of program
registerRegisterGarbage ValueLocalWithin the function

auto Storage Classs

It is a default storage class. The scope of an auto variable is limited with the particular block or function only in which they are defined. Keyword auto is used to define an auto storage class. By default, it is assigned a garbage value by the compiler whenever declared.

Example:-

storage_class    var_data_type       var_name;
auto int age;

extern Storage Class

extern storage class tells compiler that the variable is defined elsewhere and not within the same block where it is used. The variables declared as extern are not allocated any memory. It is only a declaration. It points the variable name at a storage location that has been previously defined.

Keyword extern is used to define extern storage class. Variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Extern variable cannot be initialized, it has already been defined in the original file.

static Storage Class

static storage class is used to declare static variable. It tells compiler that to keep a local variable in existence during the life-time of the program. static variables preserve the value of their last use in their scope.  Their scope is local to the function to which they were defined.

The keyword static is used to declare static storage class. Global static variables can be accessed anywhere in the program. By default, static variable is assigned the value 0 by the compiler.

register Storage Class

register storage class is used to declare register variables. It allocated the memory into the CPU registers (instead of RAM) depending upon the size of the memory remaining in the CPU. It is the compiler’s choice whether or not, the variables can be stored in the register. By default, register variables are assigned the value 0 by the compiler.

The keyword register is used to declare register storage class. It is similar to the auto storage class. The only difference is that the variables declared using register storage class are stored inside CPU registers instead of RAM. Register has faster access than that of the main memory (RAM). The access time of the register variables is faster than the automatic variables.

Sharing is Caring
Scroll to Top