Wednesday, 27 February 2013

Write a note on storage classes with examples.

The “storage class” of a variable determines whether the item has a “global” or “local” lifetime. C calls these two lifetimes “static” and “automatic.” An item with a global lifetime exists and has a value throughout the execution of the program. All functions have global lifetimes.
Automatic variables, or variables with local lifetimes, are allocated new storage each time execution control passes to the block in which they are defined. When execution returns, the variables no longer have meaningful values.
C provides the following storage-class specifiers:

Syntax
storage-class-specifier :
auto
register
static
extern
typedef

auto is the default storage class for local variables.
                        {
                            int Count;
                            auto int Month;
                        }
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

      ii)               register - 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 (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).
   {
     register int  Miles;
   }
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.

iii)           static - Storage Class

static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.
     static int Count;
     int Road;
            iv)        extern - storage Class
extern defines a global variable that is visible to ALL object modules. When you 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.
   Source 1                                                            Source 2
                   ------------                                                             --------
       extern int count;                                                           int count=5;
        write()                                                                            main()
        {                                                                                     {
          printf("count is %d\n", count);                    write();
        }                                                                                     }

The placement of variable and function declarations within source files also affects storage class and visibility. Declarations outside all function definitions are said to appear at the “external level.” Declarations within function definitions appear at the “internal level.”

The exact meaning of each storage-class specifier depends on two factors:
·                     Whether the declaration appears at the external or internal level
·                     Whether the item being declared is a variable or a function