Storage Classes :
In C all
variables have a data type, they also have a storage classes .the following
variable storage classes are most relevant to functions :
1. automatic variables
2. external variables
3. static variables
4. register variables
The scope of variable determines over what
region of the program a variable is actually available for use
Longevity refers to a period during which a
variable retains a given value during execution of a program. The visibility refers to the accessibility of a
variable from the memory.
The
variables may also be broadly categorized , depending on the place of their
declaration ,as internal or external. internal
variables are those which are declared with in a particular function ,while
external variables are declared
outside of any function.
AUTOMATIC VARIABLES :
·
Automatic variables are declared inside a
function in which they are to be utilized .
·
They are created when the function is called and
destroyed automatically when the function is exited .
·
These are private to a function in which they
are declared because of this property ,
automatic variables are also referred to be as local or internal variable .
·
A variable declared inside a function without storage
classes specifications is, by default, an automatic variable .
Example:
main()
{
int number ; /* by default number is automatic variable*/
…..
…..
}
We may also use a keyword auto to
declare automatic variables
main ()
{
auto int number;
--
--
}
External variables:
·
These are
both alive and active through out the
entire program are known as external variables.
·
They are also known as global variables.
·
They can be accessed by any function in the
program.
·
External variables are declared out side the
function.
Example:
int number;
float length=7.5;
main()
{
---
----
}
function1()
{
----
}
External
Declaration :
main()
{
----
extern
int y; /* external declaration */
---
----
}
function1()
{
extern int
y; /* external declaration */
--
}
int y;
/* definition*/
·
The External Declaration of y inside the functions informs the
compiler that y is an integer type defined somewhere else in the program.
·
Extern declaration does not allocate storage
space for variables. In case of arrays, the definition should include their
size as well.
Static
Variables :
·
Static variables
persists until the end of the program. A variable can be declared static using
the key word static.
static int x;
·
A Static
Variable may be either an internal type or external type depending on
the place of declaration.
·
Internal static variables are those which are
declared inside the function.
·
The scope of internal static variables extend up
to the end of the function in which they are defined. Therefore, they are
similar to auto variables.
·
They remain in existence (alive) through out the
remainder of the program . It can be used to count the number of calls made to
a function.
·
An external static variable is declared outside
of all functions and is available to all the functions in that program.
·
Ex:
void
ex_of_static ()
{
static
int m=0;
m++;
printf(“m
in function =%d”, m);
}
void main()
{
static int n=5;
printf(“ n in main = %d”, n);
ex_of_static();
n++;
printf(“
n in main = %d”, n);
ex_of_static();
}
OUTPUT:
n in main =
5
m in function = 1
n in main = 6
m in function = 2
Register variables :
·
We can tell the compiler that a variable should
be kept in one of the machine’s registers instead of keeping in the memory
where normal variables are stored.
·
Register variables can be accessed faster than memory variables.
·
Most of the compilers allow only int or char
variables to be placed in the register.
·
Ex : register int x;
No comments:
Post a Comment