Storage Classes
Storage Classes
Storage classes
Storage classes
C language support 4 types of storage classes and these are used to specify the
scope of different variables defined with in function blocks and program.
Auto
Static
Extern
Register
2
Auto
i. Auto
The variables without any storage class specification.
These variables are given only temporary memory space and after the execution, all the automatic variables
will get disposed.
Automatic variables are allocated memory automatically at runtime.
The visibility of the automatic variables is limited to the block in which they are defined.
The scope of the automatic variables is limited to the block in which they are defined.
The automatic variables are initialized to garbage by default.
The memory assigned to automatic variables gets freed upon exiting from the block.
3
Auto
Output
4
Static
The variables defined as static specifier can hold their value between the multiple function calls.
Static local variables are visible only to the function or the block in which they are defined.
A same static variable can be declared many times but can be assigned at only one time.
The visibility of the static global variable is limited to the file in which it has declared.
5
Static
The external storage class is used to tell the compiler that the variable defined as extern is declared with an
external linkage elsewhere in the program.
The variables declared as extern are not allocated any memory. It is only declaration and intended to specify
that the variable is declared elsewhere in the program.
We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any
block or method.
An external variable can be declared many times but can be initialized at only once.
If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in
the program which may be extern or static. If it is not, then the compiler will show an error.
7
Extern
output:
x in file1=10
x in file2=10
8
Register
The register storage class is used to define local variables that should be stored in a register instead of RAM.
Since the variable is stored in a CPU register, the maximum size of the variable is equal to the register size.
The register keyword is used to declare register variables. Register variables were supposed to be faster than local
variables.
However, modern compilers are very good at code optimization, and there is a rare chance that using register
variables will make your program faster.
9
Register
OUTPUT:
10