p

Saturday, 29 October 2011

Storage Classes and their Properties in C Programming Language

Storage class refers to the permanence of a variable and its scope within the program. i.e. the portion of the program over which the variable is recognized. There are four type storage classes in c programming language. They are with their keyword:
  • Automatic, keyword: auto
  • External, keyword: extern
  • Static, keyword: static
  • Register, keyword: register
Example: Shown below are several typical variable declarations that include the specification of a storage class:
  • auto int  a;
  • extern char name;
  • static float pi;
  • extern double value;
The first declaration states that ais a automatic integer variable. Second declaration stats that name is the external character variable. Third declaration states that pi in a static floating point variable. And last variable value ie a external double variable.
Automatic storage class: auto is the default storage class for local variables. Some features of automatic storage class:
  • Storage: storage will be in memory.
  • Value: garbage value.
  • Scope: scope is local to block to the variable.
  • Life: till, controls remain within the block.
External storage class: extern defines a global variable that is visible to all object modules. When we use ‘extern’ the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined. Features of External variable:
  • Storage: memory.
  • Value: zero.
  • Scope: local to block.
  • Life: till controls remains within the block.
Static Storage Class: static is the default storage class for global variables. ‘static’ can also be defined within a function. If this is done, the variable is initialized at compilation time and retains its value between calls. Because it is initialized at compilation time, the initialization value must be a constant. This is serious stuff tread with care. Features of static storage class:
  • Storage: memory.
  • Value: zero.
  • Scope: local to block.
  • Life: Till control remains within the block.
Resister Storage Class: register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size and can’t have the unary ‘&’ operator applied to it. Register should only be used for variables that require quick access such as counters. It should also be noted that defining ‘register’ goes not mean that the variable will be stored in a register. It means that it might be stored in a register depending on hardware and implementation restrictions. Features of resister class:
  • Storage: register.
  • Value: garbage value.
  • Scope: local to the block.
  • Life: value persists between variable.