Storage class or type defines unique characteristics and scope of a variable. There are mainly five types of storage types available in C and they are -

  • Global
  • Static
  • Automatic
  • Register
  • Extern

Global As the name suggests, global variable can be accessed universally. There is no need to give this keyword if we declare a variable outside function brackets. The visibility of a global variable starts from the next line of the declaration and can be accessed throughout the file. Life of a global variable is till the program ends. Global variables are by default zero if not initialized. Global variable sometimes get extended with extern to export to be used by external modules or files.

Static Scope of a static variable is local to module/file if declared in file scope or scope will be local to the function if declared inside a function. Life of a static variable is till the program ends. Global and static both are linked in .DATA or .BSS area but in case of static linker never exposes these variables to outside. Static variables are by default zero if not initialized.

Automatic Since the type is named as automatic, this keyword is not needed before a variable. A variable declared as a local scope or declared in a function can be treated as automatic. Life time of an automatic variable in only till the function ends and also the visibility is only inside the function or section. Automatic variable are linked to take the memory in current stack area and can have any value if not initialized.

Register Register keyword tells compiler to keep this variable in one of register of CPU to optimize execution time. This keyword never ensures that this will be done. If there are free registers available to hold this variable then only the variable can be treated as register type. Only automatic variables can be optimized as Register type. Global/extern/static cannot be optimized with register.

Extern External variables are those global variables with Extern type so that it can be accessed by external or third party modules. Life, scope and default un-initialized value of extern variable is same as global. Life is till program ends and scope is global or external.

We have summarize above discussion in a single table as below

Class ScopeInit ValueLife
Global UniversalZeroTill Program exits
Static Local to function/fileZeroTill Program exits
Automatic Local to functionany randomTill Function exits
Register Local to functionany randomTill Function exits
Extern Universal and external modulesZeroTill Program exits

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

pre-processing, compilation, optimization, linking format, debugging with GDB, structured programming language, data types, storage classes, volatile variable, external vs static, global vs static variable, register vs auto variable,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan