Storage Classes in C Programming Language

A storage class in C is an attribute that tells us where the variable would be stored, what will be the initial value of the variable if no value is assigned to that variable, life time of the variable and scope of the variable.

There are four storage classes in C:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class

Automatic storage class in C:
The keyword used for Automatic storage class is ‘auto’.
The variable declared as auto is stored in the memory.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Example:

Output:
1285
As seen above, the output is garbage value.

Register storage class in C:
The keyword used for Register storage class is ‘register’.
The variable declared as register is stored in the CPU register.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable id defined.
Main difference between auto and register is that variable declared as auto is stored in memory whereas variable declared as register is stored in CPU register. Since the variable is stored in CPU register, it takes very less time to access that variable. Hence it becomes very time efficient.
It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable.

Example:

Output:
4587
As seen above, the output is garbage value.

Static storage class in C:
The keyword used for Static storage class is ‘static’.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is local to the block in which the variable is defined.
Life of variable persists between different function calls.

External storage class in C:
The keyword used for External storage class is ‘extern’.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is global.
Variable is alive as long as the program’s execution doesn’t come to an end.

External variable can be declared outside all the functions or inside function using ‘extern’ keyword.
Example:

Output:
0 10

Also see:
Comparison of Automatic, Register, Static and External Variables Storage Classes in C

You may also like...

45 Responses

  1. Steve says:

    I was searching on internet for information on storage classes in C. I got link to this site. This site is really amazing. Has explanation of every topics in details. Really interesting site. Learn C Online will definitely prove helpful for new C learner. I really appreciate the hard work put up by the author of this site. Hats off to you for creating such a useful site on C Programming language. The author of learnconline.com is very helpul. Helped me understand a topic on C Programming through email. Really appreciate this.

  2. Anonymous says:

    very nice 🙂

  3. Is storage classes 4 or 5

  4. Learn C Online site is useful to learn C programming. THIS SITE IS ONE OF BEST SITE IN NOW A DAYS.
    THANKS TO AUTHOR AND THANKS TO SITE DESIGNER AND ALL OF US WORK FOR THIS SITE.

  5. preet singh says:

    this is very amezing

  6. Anonymous says:

    very nice

  7. sadiq ullah says:

    tnx a lot 4 giving us such a nice methode to undrstand this topic

  8. Anonymous says:

    many many thanks 2 online c 4 this wonder ful site enabling us to understand the concepts in c language clearly in a simple fashion.

  9. Anonymous says:

    what ois the memory location where external storage is stored

  10. Can u give an example code for Static Storage class? And as u ve said that it’s life cycle persists for diff func call, then will it be initialized again for the next func call?

  11. ASRAAR AHMAD says:

    I am really impressed , this site covers all the basics and it is very helpful for beginners

    Thanks !

  12. Anonymous says:

    Awesome tutorial , all concepts refreshed

  13. Ravi Shukla says:

    best satisfactory knowledge i got only through this site

  14. Anonymous says:

    thanks a lot :)…its very useful

  15. Vaisakh Jain says:

    this is what we expected from a tutorial site. Superb!!!

  16. Anonymous says:

    a very good site for the begginers

  17. why is ‘v’ of Void is in capital letter in Void main(). Please explain difference between
    1) void main()
    2) Void main()

  18. LearnCOnline says:

    Thanks for pointing out the mistake. It should be small ‘v’. Made the correction.

    Thanks,
    LearnCOnline Team.

  19. Rajavel R says:

    very nice useful website…. thank you..

  20. WAQAR SABIR says:

    I am very impressed by this site,

  21. hariom saini says:

    very nice,understand for good site.

  22. why are storage classes called so when there is no concept of classes in C?

    • shiva kumar reddy says:

      there is no like between storage class and c class, sorage class is which give the details about a variable and c class a class of similer variables.

  23. site is very useful for doing assignments ….good job

  24. himani says:

    very nice and useful site

  25. Anonymous says:

    Thanks for this useful post, it really helped me.
    May God Bless You!!!

  26. Anonymous says:

    semi-colon is missing in printf statements ???

  27. Nikitha says:

    hi,
    there is a small mistake in this the explanation under the external storage class u mentioned “The variable declared as static is stored in the memory. ” it should be like this “The variable declared as extern is stored in the memory”.

  28. I got the clear concept from this site.thank u……

  29. The concept is given in clear cut manner.It is very useful for me.

  30. I got clear idea about the storage classes.Thank u….

  31. what a nyc way of writing…really easy to understand…!!!!!

  32. Anonymous says:

    This site is a very useful site
    zargarany

  33. Anonymous says:

    Thanx

  34. Anonymous says:

    thank you. this site is really helpful

  35. THULASI HARI says:

    very useful site.tq so much

  36. very nice thankyou so much

  37. Anonymous says:

    in External storage class you have mentioned abt static ( 2nd line ).

    Good post 🙂

  38. As far as I know, the static storage class is the default for all global variables but the keyword ‘static’ can be used for any variables but that will prevent its value to be changed. The extern storage class is best used in order to use variables from one program in another program. But like this site says, it makes the scope of the variable to be global.
    Anyways, nice work and thanks a lot!! 🙂

  39. Ok, as a correction: when variables are declared as static, they don’t need to be initialized again, at all.
    Eg. if a variable is defined static and given a value inside a function, then, when the second time that function is called, the value at the end of the first call would persist and that variable will not get initialized after its first initialization.

  40. Gopal says:

    Good explanation but In External storage class explanation i think by mistake mentioned about static keywords.

Leave a Reply

Your email address will not be published. Required fields are marked *