What are storage classs in C

Following are the storage classes in C.

Auto, Extern, Register, Static

In C programming variable does have storage classes with their data types. Default storage class is  taken as auto. Storage class defines variables visibility and accessibility.

Auto :  Whenever we define a variable inside functions ,they are automatically created and destroyed while function is called. These varibles are auto variables. Whenever we define a variable like datatype varname; (e.g. int a;) then variable a has default storage class as auto.

Auto variable in Functions:

When we declare auto varibles within a function they are created and destroyed automatically. They are not accessible by outside of function.


In below functions both declaration of c is same .


Extern: extern keyword is used to reference a global varible or functions in same file or in other file. Declared variable/functions using extern keyword is cannot be initialized because its value depends on previous declaration.

Note : extern declaration doesnot allocate storage space for variable.


Use of extern in same file



In avobe code extern declaration of varible a refers to the global variable a;

Referencing global varible of other file.



File 2.c

On compiling both files together :

gcc File1.c File2.c



Static:  The local variable declared in function doesnot retain its previous value when function is called again but static variable retains its value.Static variables are initialized only one when they are declared. Static variable have lifetime through out the life of program. Static variable can be global or local it depends on its place of declaration where it is declared.

Comparing two functions with static and non static variable :

Function without static variable:



0 0

Function with static variable:



0 1

We can see the difference in output just using static keyword infront of  variable c.

Register : As far we have seen all variable declrade are stored in RAM ( Memory) ,but using register keyword we can use one of the register space from CPU.Register variables are faster than memory variables they can perform faster execution.  We should not declare many register varibles, we can use certain number of register variables.



%d bloggers like this: