A storage class defines the scope and life time of variables.
There are four storage classes:
1.auto - Storage Class
auto storage class is the default for local variables.
example :
{
int a;
auto int M;
}
The example above defines two variables with the same storage class. Auto Storage class only be used within the functions, i.e. local variables.
2.register - Storage Class
register Storage Class is used to define local variables and to stored in a register instead of RAM.
{
register int Miles;
}
Register must be used for variables that need quick access - such as counters.
3.static - Storage Class
static storage class is the default for global variables.
static int Count;
int Road;
{
printf("%d\n", Road);
}
static variables are 'visible' to all functions in this source file.
The variable with static storage class will retain its value during various call.
void func(void);
static count=10; /* Global variable - static storage class*/
main()
{
while (count--)
{
func();
}
}
void func( void )
{
static i = 5;
i++;
printf("i is %d and count is %d\n", i, count);
}
This will produce following result
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
4.extern - Storage Class
extern storage class is used to give a reference of a global variable.
'extern' variable cannot be initialized.
There are four storage classes:
- Auto Storage class
- Register Storage class
- Static Storage class
- Extern Storage class
1.auto - Storage Class
auto storage class is the default for local variables.
example :
{
int a;
auto int M;
}
The example above defines two variables with the same storage class. Auto Storage class only be used within the functions, i.e. local variables.
2.register - Storage Class
register Storage Class is used to define local variables and to stored in a register instead of RAM.
{
register int Miles;
}
Register must be used for variables that need quick access - such as counters.
3.static - Storage Class
static storage class is the default for global variables.
static int Count;
int Road;
{
printf("%d\n", Road);
}
static variables are 'visible' to all functions in this source file.
The variable with static storage class will retain its value during various call.
void func(void);
static count=10; /* Global variable - static storage class*/
main()
{
while (count--)
{
func();
}
}
void func( void )
{
static i = 5;
i++;
printf("i is %d and count is %d\n", i, count);
}
This will produce following result
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
4.extern - Storage Class
extern storage class is used to give a reference of a global variable.
'extern' variable cannot be initialized.
0 comments:
Post a Comment