Storage Class

8 – Storage Classes in C Language

Storage Class define the Scope (visibility), life-time of variables and/or functions within a C program. We have (4) four different classes in C program.

  • auto
  • register
  • static
  • exam

The auto Storage Class

It is the default storage class for all variables.

The above example defines two variables within the same storage class. The ‘auto‘ can only be used within functions i.e local variables.

The register Storage Class

It is used to define local variables which should be stored in a register instead of RAM. This means that variable has maximum size equal to the register size (Usually one word) & can’t have Unary ‘&’ operator applied to it (as it does not have a memory location).

The registers should only used for variables that requires quick access such as counters. It should also be noted that defining registers does not mean that variables will be stored in a registers. It means that it might be stored in a register depending on Hardware & implementation restrictions.

The static

This static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of making & destroying it each time. Therefore, making local variables static allows them to maintain their vales between function call.

This modifier also applied to global variables. When this is done, it cases the variable’s scope should be restricted to the files in which it is declared.

In C programming language, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

The result of the above code, after compilation & execution.

The extern Storage Class

This storage class gives reference of a global variable which is visible to all the program files. When you ‘extern’ the variable cannot be initialized, however it points the variable name at a storage location that has been previously defined.

When you have multiple files & you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide reference of the define variable or function.

Just for understanding, extern is used to declare a global variable or function in another file.

Extern modifier is most commonly used when there are two or more Files sharing the same global variables or functions as explained below:

Here extern is being used to declare count in the second file, Where as it has its definition in the first file, main.c . Now compile these two files

Subscribe our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.