Storage Classes in C notes
Storage Classes in C notes
Register (Examples)
NOTE: A variable is not only associated with a data type, its value but
also a storage class.
The scope of an auto variable is limited with the particular block only.
Once the control goes out of the block, the access is destroyed. This
means only the block in which the auto variable is declared can access
it.
int add(void) {
int a=13;
auto int b=48;
return a+b;}
We take another program which shows the scope level “visibility level”
for auto variables in each block code which are independently to each
other:
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);}
OUTPUT:
3 2 1
• Static local variable is a local variable that retains and stores its
value between function calls or block and remains visible only to
the function or block in which it is defined.
• Static global variables are global variables visible only to the file
in which it is declared.
Keep in mind that static variable has a default initial value zero and is
initialized only once in its lifetime.
The variables declared using register storage class has no default value.
These variables are often declared at the beginning of a program.
OUTPUT:
Storage Default
Declaration Storage Scope Lifetime
Class Initial Value
Inside a Within the Within the
auto Memory Unpredictable
function/block function/block function/block
Inside a CPU Within the Within the
register Garbage
function/block Registers function/block function/block
Entire the file
and other files
Outside all where the program
extern Memory Zero
functions variable is runtime
declared as
extern
Static Inside a Within the program
Memory Zero
(local) function/block function/block runtime
Static Outside all program
Memory Zero Global
(global) functions runtime