Storage Classes in C

There are two ways to characterize variables: by data type, and by storage class.

  • • data type refers to the types of information represented by a variable, for example, integer number, floating – point number, or a character
  • • storage class refers permanence of variables and its scope within the program that is, the portion of program over which the variable is recognized.

There are four storage class specifiers supported by C:

auto
static
register
extern

auto

  • auto variable declared within a function and are local to the function in which they are defined
  • • Any variable declared within a function is interpreted as an auto unless a different storage class specified
  • • This includes formal argument declarations
  • auto variables defined in different functions are independent to each other, even through they have the same name
features
Storage        main memory
Default Initial value   garbage
Scope           local to the block in which it is defined 
Life            as long control within the block in which it is defined

Static

Static variables are permanent variables within their own function or file. Unlike global variables, they are not known outside their function or file, but they maintain their values between calls. This feature makes it useful.

features
Storage        main memory
Default initial value   0
Scope           local to the block in which it is defined
Life            retains its value between function calls (or files)
static local variables
  • • the compiler creates permanent storage, as it creates storage for a global variable, when a static modifier to a local variable is declared.
  • • A static local variable can also be initialized
Static global variable

Applying the specifier static to a global variable instructs the compiler to create a global variable that is known only to the file in which it is declared. This means that even though the variable is global, routines in other files may have no knowledge of it or alter its contents directly.

The key difference between a static local variable and a global variable is that a static local variable is a local variable that retains its value between function calls.


register

  • • the register declaration advises the compiler that the variable will be heavily used.
  • • the register storage specifier traditionally applied only to variables of type int and char.
  • • the register specifier requested that the C compiler keep the value of the variable declared with this specifier in the register of the CPU rather than the memory. This meant that operations on a register variable could occur much faster than on a variable stored in memory because the value of the variable was actually held in the CPU and did not require a memory access to determine or modify its value.
  • • register specifier can be applied only to local variables and to the formal parameters in a function.
  • • global register variables are not allowed.
  • • the number of register variables optimized of speed allowed within any one code block is determined by both the environment and the specific implementation of C. C compiler automatically transforms register variables into nonregister variables when too many register variables are declared
  • • since the register variable are stored in the registers of the CPU, they do not have addresses. So address of register variable using & operator is not allowed.
features
Storage              Registers of CPU
Default initial value           Garbage
Scope                   Local to the block in which to defined 
Life    As long as the control remains within the block in which it is defined

extern

Because C allows separately compiled modules of a large program to be linked to speed up compilation and help manage large projects. There must be some way telling all the files about the global variables required by the program. A global variable can be declared once only. If a global variables will be declared more than once in the same name, then there will be an error message. The same problem will occur, if all the global variables needed by the program will be declared in each file. The solution is to declare all global variables in the file and use extern declaration in other.

features
Storage              Main memory
Default                 O
Scope                   Global
Life                    As long as program is an execution

extern variable declarations can occur inside the same file as the global declaration, but they are not necessary.

void main ()
    {
         extern int x;
         .    .    .    .    .
          .    .    .    .    .
          .    .    .    .    .
    }


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext