Thursday, 2 February 2012

Comparison storage classes

In C, storage class provides information about their location and visibility.
C provides four storage classes as:
  1. auto storage class
  2. register storage
  3. extern storage class
  4. static storage class
  • Storage class determines its storage duration, scope and linkage.
  • Storage duration is the period during which that identifier exists in the memory.
  • Scope is where the identifier can be referenced in a program.
  • Linkage determines for a multiple-source file.
Let's summarised all storage classes in C as following as:

    Features Automatic Storage Class Register Storage Class Static Storage Class External Storage Class
    Keyword auto register static extern
    Initial Value Garbage Garbage Zero Zero
    Storage Memory CPU register Memory Memory
    Scope scope limited,local to block scope limited,local to block scope limited,local to block Global
    Life limited life of block,where defined limited life of block,where defined value of variable persist between different function calls Global,till the program execution
    Memory location Stack Register memory Segment Segment
    Example

    void main()
    {
      auto int i;
      printf("%d",i);
    }
    OUTPUT
    124


    void main()
    {
      register int i;
      for(i=1; i<=5 ; i++);
        printf("%d ",i);
    }
    OUTPUT
    1 2 3 4 5


    void add();
    void main()
    {
      add();
      add();
    }
    void add()
    {
      static int i=1;
      printf("\n%d",i);
      i=i+1;
    }
    OUTPUT
    1
    2


    void main()
    {
      extern int i;
      printf("%d",i);
      int i=5
    }
    OUTPUT
    5
    FIGURE: Table of Storage classes in C
    You might also like to read:

    1. What is storage classes in C
    2. Automatic storage class
    3. Register  storage class
    4. Static  storage class
    5. External  storage class

    No comments:

    Post a Comment