Storage classes

Chapter 8:Preprocessor And Complex Declarations

    Storage classes

  •           In C type and storage class are the properties of a variable. Type refers to the data type of variable .It may be a character or integer etc. And storage class decides the part of memory where storage space is allocated for an object and how long it stays in existence. The types of storage class are automatic, external, static, and   register.

    Automatic storage class

              The keyword used for Automatic storage class is ‘auto’. It indicates that the memory location of a variable is temporary. Because a variable's reserved space in the memory can be removed or rearranged when the variable is out of its scope. Variables declared inside the function body are automatic by default. These variables are also known as local variables. They are local to the function. They don’t have meaning outside the function. The auto keyword is rarely used. Default value of that variable is garbage value.

    Example:

    #include < stdio.h >
    #include < conio.h >
    void main()
    {
        auto int z;
        printf(“%d”,z)
    }
    Output:

              1285

    External storage class

              The keyword used for External storage class is ‘extern’. External variables are declared outside all functions. Any function can access those external variables. They are also identified as global variables. Default initial value of that variable is zero. Variable is alive as long as the program’s execution doesn’t come to an end.

              Consider a large program that contains many files. If the global variable is declared in file aaa and that variable is used in file bbb then, compiler will cause error. To overcome this trouble, keyword extern is used in file bbb to point out that, the variable specified is global variable and declared in another file.

    Example:

    #include < stdio.h >  
    void test();
    int x=15;    /* x is global variable because it is outside every function */
    int main()
    {
        x+=4;
        test();
        return 0;
    }
    void test()
    {
       ++x;     /* Variable x is not declared in this function but, works in any function as they are global variable  */
       printf("x=%d\n",x);
    }
    Output

              x=20

    Register Storage Class

              The keyword used for Register storage class is ‘register’. Register variables are similar to automatic variable and exists inside that particular function only. Each computer has a certain number of registers to hold data and perform arithmetic or logical calculations. The variable declared as register is stored in the CPU register rather than memory. Values stored in register are much faster than that of memory. Therefore, it may help to speed up the program. If there is no register available it will automatically store it in memory. Default value of that variable is garbage value.

    Example:

    #include < stdio.h >
    #include < conio.h >
    void main()
    {
        register int t;
        printf(“%d”,t)
    }
    Output:

              4587

    Static Storage Class

              The keyword used for Static storage class is ‘static’. The static keyword can be used to variables with either block scope or program scope. A static variable has a permanent duration. The value of the variable is also continued outside the scope. If execution returns to the scope of the variable, the last value stored in the variable is still there. The variable declared as static is stored in the memory. The value of static variable persists until the end of the program. Default value of that variable is zero.

    Example:

    #include  < stdio.h >
    void Test();
    int main()
    {
       Test ();
       Test ();
       Test ();
    }
    void Test ()
    {
        static int i=0; //Here, i is a static variable.
        printf("%d\t",i);
        i+=10;
    }
    Output

              0      10     20

              During first function call, it will display 0. Then, during second function call, variable i will not be initialized to 0 again, as it is static variable. So, 10 is displayed in second function call and 20 in third call.
    If c automatic variable, then output: 0    0     0

© 2015 by Learncertification All Rights Reserved. The certification names are the trademarks of their respective owners. Terms & Privacy Policy