Tuesday 30 January 2018

Storage Classes in C

Storage Classes in C

            We have seen that every variable has a data type. To fully define a variable, we need a storage class as well apart from data type. But you might be thinking that we haven't used any storage class so far. Actually, we were using a default storage class till now. When we do not define a storage class, compiler automatically assigns a default storage class to it. A variable's storage class gives information about the storage location of variable in memory, default initial value, scope of the variable and it's lifetime.
Storage class specifiers supported in C are auto, register, static and extern.

' auto ' storage class
            
            This is the default storage class we have been using so far. It applies to local variables only and the variable is visible only inside the function in which it is declared and it dies as soon as the function execution is over. If not initilaized, variables of class auto contains garbage value.
Following example shows how variables of auto storage class are declared :



' register ' storage class

            Variables of class 'register' are stored in CPU registers instead of memory which allows faster access. The scope of the variable is local to the function in which it is defined and it dies as soon as the function execution is over. It contains some garbage value if not initialized. It is declared as :

 

' static ' storage class

           The scope of static variable is local to the function in which it is defined but it doesn't die when the function execution is over. The value of a static variable persists between function calls. The default initial value of static variable is 0. Following program illustrates the behaviour of static variables : 

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :
 
Calling Function :-
Function is called 1 times
Calling Function :-
Function is called 2 times
Calling Function :-
Function is called 3 times

----------------------------------------------------------------------------------------------------------------------------

' extern ' storage class

          Variables of extern storage class have a global scope. We use extern variables when we want a variable to be visible outside the file in which it is declared. So, an extern variable can be shared across multiple files. An extern variable remains alive as long as program execution continues. A static global variable is visible only in the file it is declared but an extern global variable is visible across all the files of a program.
We have used declaring and defining a variable interchangeably but extern storage class adds a distinction.

In simple words :



 The declaration and definition of a variable can be in different files.
Following program shows the use of extern storage class :
----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

           We can observe that the variable count is declared in one file and it is defined in other file. It can be accessed and modified in any of the files and the updated value of count is reflected in both the files.

 

No comments:

Post a Comment