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:
#include<stdio.h> #include<conio.h> int main(){ auto int a; printf(“%d”,a) return 0; }
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:
#include<stdio.h> #include<conio.h> int main(){ register int a; printf(“%d”,a) return 0; }
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:
#include<stdio.h> #include<conio.h> int a; int main(){ extern int b; printf(“%d %d”,a,b) return 0; } int b=10;
Output:
0 10
Also see:
Comparison of Automatic, Register, Static and External Variables Storage Classes in C
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.
very nice 🙂
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.
this is very amezing
very nice
tnx a lot 4 giving us such a nice methode to undrstand this topic
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.
what ois the memory location where external storage is stored
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?
I am really impressed , this site covers all the basics and it is very helpful for beginners
Thanks !
Awesome tutorial , all concepts refreshed
best satisfactory knowledge i got only through this site
thanks a lot :)…its very useful
this is what we expected from a tutorial site. Superb!!!
a very good site for the begginers
why is ‘v’ of Void is in capital letter in Void main(). Please explain difference between
1) void main()
2) Void main()
Capital ‘V’ is wrong. C programming is case sensitive. we can only use void main()
{
…
…
…
}
Thanks for pointing out the mistake. It should be small ‘v’. Made the correction.
Thanks,
LearnCOnline Team.
very nice useful website…. thank you..
I am very impressed by this site,
very nice,understand for good site.
why are storage classes called so when there is no concept of classes in C?
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.
site is very useful for doing assignments ….good job
very nice and useful site
Thanks for this useful post, it really helped me.
May God Bless You!!!
semi-colon is missing in printf statements ???
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”.
I got the clear concept from this site.thank u……
The concept is given in clear cut manner.It is very useful for me.
I got clear idea about the storage classes.Thank u….
good
what a nyc way of writing…really easy to understand…!!!!!
This site is a very useful site
zargarany
Thanx
thank you. this site is really helpful
very useful site.tq so much
very nice thankyou so much
AMAZING!!!:)
in External storage class you have mentioned abt static ( 2nd line ).
Good post 🙂
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!! 🙂
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.
Good explanation but In External storage class explanation i think by mistake mentioned about static keywords.