C - STORAGE CLASS SPECIFIERS
STORAGE CLASS SPECIFIERS –
A variable name identifies some physical
location within the computer where the string of bits representing the
variables value is stored. A variable’s storage class tells us:
1.
Where the variable would be stored.
2.
What will be the initial value of the variable, if the initial
value is not specifically assigned.
3.
What is the scope of variable.
4.
What is the life of the variable, i.e. how long would the variable
exist. A variable in C can have any one of the four storage classes.
a.
Automatic variable
b.
External variable
c.
Static variable
d.
Register variable
AUTOMATIC VARIABLE
Auto variable stored in RAM. This is the
default storage class and the keyboard auto in used to declare variable. Auto
variable are active in a block in which they are declare.
EXAMPLE
main()
{
auto int a=20;
{
auto int a=30;
printf("Value of inner block %d",a);
}
sample();
printf("Value
of inner block %d",a);
getch();
}
sample()
{
auto int a=40;
printf("Value
of a function block %d",a);
}
EXTERN VARIABLE
In this real-life programming environment, we
may use more one source file which may be compiled separately and linked later
to form an executable object code. Multiple source files share available
provide. It is declared as an external variable appropriately. Variable that
are shared by two or more files are global variable and we must declare them
accordingly in one file and then explicitly define them with extern in another
file.
EXAMPLE-
/* sem.c */
int I;
fun();
{
printf(
“\nHello”);
}/
* bca.c */
#include<sem.c>
main()
{
extern int I;
printf(“%d”,i);
fun();
getch();
}
STATIC STORAGE CLASS
Variable declared in this class are also
stored in the RAM. The keyword static is used to declare these variables.
Similar to auto variable, the static variable is also active in the block in
which they are declared and they retain the latest value. The static variable is
commonly used along with function.
EXAMPLE-
#include<stdio.h>
#include<conio.h>
main()
{
show();
show();
show();
show();
getch();
}
show()
{
static int i=1;
printf(“%d”,i);
i++;
}
REGISTER STORAGE CLASS
Variable declared using this class are stored
in the CPU memory register. They keyword register is used to declare these
variables only a variable using this class to improve the program execution
speed. The behavior of register variable is similar to that of auto variable
that their storage location is different in case of non-availability of CPU
memory register, these variables are stored in RAM as auto variable.
EXAMPLE-
main()
{
register int a=6;
register char b=‘y’;
printf(“Value of a =%d”,a);
printf(“Values of ch =%c”,b);
getch();
}
Output-
Value of a = 6
Value of ch = y
Comments
Post a Comment