Introduction:
Data type declaration instruction depicts detail about:
1) Name
of the variable
2) Size
of memory block
3) Type
of the content it can store
For example
main()
{
int x;
…
}
Here we can easily understand that the variable name is ‘x’.
Size it takes is two bytes. Type of the content it can store is integer. So we
can say data type declaration instruction gives few basic properties of
variable. This can be understood at the time of declaration only.
Now we are going to learn, how we can have more information
about variable properties.
A variable of the program belongs to one of the four storage
class. This makes variable a little variation in its properties. These storage
classes talk about four properties:
►Storage
► Default
value
► Scope
► Life
Storage Classes:
1) Automatic
Storage Class
2) Register
Storage Class
3) Static
Storage Class
4) External
Storage Class
Automatic Storage
class:
► Key
word used is auto
► Storage
in main memory (RAM)
► Default
value is garbage
► Scope
is limited to the block in which it is declared.
► Life
retains till the control moves in the block in which it is declared
Example:
main()
{
auto int x = 2;
printf(“%d ”, x);
{
auto int x = 5;
printf(“%d ”, x);
}
printf(“%d ”, x);
}
Output is 2 5 2
Some strange things, like declaration of x two times and
declaration instruction after printf(). You remember the data type declaration
instruction is possible only if it occur before any other action statement. But
this rule has a scope within the block. We are using two blocks. Second
declaration is the first statement in the inner block hence it is perfectly
legal. Scope and life of second x is limited to the block hence can not be
accessed outside.
Register Storage
Class:
► Keyword
used is register
► Storage
in register
► Default
value is garbage
► Scope
is limited to the block in which it is declared.
► Life
retains till the control moves in the block in which it is declared.
Register storage is a request only, allotment of register to
the variable is depend on the availability of register which is monitored by
operating system. Sometimes we feel a particular variable is used frequently, in
such cases we do request to the operating system to hold its data in the
processor register so the program execution becomes faster.
Static Storage Class:
► Keyword
used is static
► Storage
in main memory
► Default
value is 0 (zero).
► Scope
is limited to the block in which it is declared.
►Life
retains till the execution of program.
Example:
main( )
{
inc( );
inc( );
}
inc( )
{
static int
x;
printf
(“%d”, ++x);
}
Output is
12
In this program, x is a static variable in function inc().
Static variables are created only once in a program but its life time is whole
program. Scope of static variable is limited to the block in which it is declared,
thus we can not access variable ‘x’ from out side the inc() function. Since the
variable remains alive in memory in subsequent function call of inc(), any
change in variable ‘x’ is available for next call to function inc().
External Storage
Class:
► Keyword
used is extern
► Storage
in main memory
► Default
value is 0 (zero).
► Scope
is global.
► Life
retains till the execution of program.
Example:
int x=5; //global variable
main()
{
printf(“%d”,
x);
fun();
}
int fun()
{
printf(“%d”,x);
}
Output is
55
External variables are declared outside all functions. They
are also known as global variables. All function in the program can access
global variable. There will be a single copy of global variable, shared between
all functions.
If any function declared a local variable with the same name
in its body, local variables get preference over global variable.
No comments:
Post a Comment