Tuesday 19 August 2014

What are the different Storage Classes in C language


Storage Classes:

                   Storage Classes specify the characteristic properties of the memory location where memory is allocated for that variable.

--> C Language Provide four types of storage classes as follows.
1) Auto Storage Class
2) Static Storage Class
3) Extern Storage Class
4) Register Storage Class

1)Auto Storage Class:

--> Here memory is allocated and de-allocated automatically based on scope of the variable.
--> When the control enters to the scope, memory is allocated.
--> When the control runs out of the scope, memory is de-allocated automatically.
--> So, scope of an automatic variable will be only for the block in which it is contained and it's time will be only for the execution of that block/function.

                                       Keyword:  auto

--> When we don't specify any storage class, then by default is auto

2)Static Storage Class:

--> It is useful when we are required to retain the values of variables even after coming out of the function.
--> for the static variables, memory is allocated only once when the program starts the execution and will be de-allocated when execution of the program is complete. 
--> So, scope of a static variable will be only for the function in which it is contained, where as life time will be throughout the execution of the program.

                                     Keyword : static

--> static variable assume a value of  "0"(zero) by default, we can also assign our interested value as required.

3)Extern Storage Class:

--> This is useful when different files in a project are required share/access a common variable.

                                    Keyword: extern

--> Only global variables can be declared as extern variables.

4)Register Storage Class:

--> It is useful when we are required to store the variable specified in a CPU register.

                                      Keyword: register






0 comments:

Post a Comment