Storage specifier in C

The C compiler provides or support four reserve word named auto, static, extern and register are known asa storage specifier.
A storage specifier specify memory space into region of RAM.
   (1)    auto :- auto storage specifier is responsible to specify memory space into stack region. an auto storage specifer only allowed with local variable.
Example- {
                auto int a;
                                or
                auto int a;
}
NOTE:- A local variable by default precede by auto storage specifier.
Example:-
void main()
{
int a=5;
auto int b=20;
clrscr();
printf(“a=%d,b=%d”,a,b);
getch();
}
Output---------à a=5,b=20

   (2)    static:-  static storage specifier is responsible to specify memory  space in global region.A static storage specifier is used as a following two ways:-
(a)    static used with global variable
(b)   static used with local variable

     (a)    static  used with global variable :- A global variable by default preceded by static storage specifier or it can be preceded with global variable manually.
Example:- int a=20;
static int b=5;
void main()
{
clrscr();
printf(“a=%d ,b=%d”,a,b);
getch();

     (b)   static used with local variable :- When we use a static storage specifier with local variable manually then it poses following properties:-
(i)                  A local variable will be allocates into global region.
(ii)                By default initialized with 0.
(iii)               It will be deallocated automatically when programs terminates.
(iv)              It will be access by only its own open({) and close(}) curly braces.
Example:-
(1)     void main()
                {
                int i=1;
                clrscr();
                while(i<=3)
                {
                printf(“i=%d \n”,i);
                {
                int ctr=0;
                printf(“ctr =%d \n”,ctr);
                ctr++;
                }
                i++;
                getch();
                }
Output---à       i=1
                                ctr=0
                                i=2
                                ctr=0
                                i=3
                                ctr=0
     (2)    void main()
{
int i=1;
clrscr();
while(i<=3)
{
printf(“i=%d\n “,i);
{
static int ctr;
printf(“ctr =%d \n “,ctr);
ctr++;
}
getch();
}
Output--------à i=0
                                ctr=0
                                i=2
                                ctr=1
                                i=3
                                ctr=2
     (3)    extern:- extern storage specifier is responsible to access a global variable before its definition.
A global variable cannot be access before its definition if we want to access a global variable before its definition then extern reserve word must be ue as afollowing format:-
extern datatype varname[=value];

 
Syntax:-
Example:-
(i)                  void disp()
{
printf(“\n value of a=%d”,a);
}
int a=10;
void main()
{
clrscr();
printf(“a=%d”,a);
disp();
getch();
}
Output------à Error: Undefined symbol ‘a’
(i)                  void disp()
{
extern int a;
printf(“\n value of a=%d”,a);
}
int a=10;
void main()
{
clrscr();
printf(“a=%d”,a);
disp();
getch();
}
Output-----à a=10
                                value of a=10
     (4)    register:- register is storage specifier is specially used for specify a memory space into register of CPU rather than RAM which enhance speed up of processing.
A register storage specifier only allowed with local variable.It is logically suggested to a register storage specifier must be used with looping driven variable.
Example:-            void main()
                                {
                                register int i=1;
                                clrscr();
                                while(i<=3)
                                {
                                printf(“Loop execution: %d: times!\n”,i);
                                i++;
                                }
                                printf(“Execution complete”);
                                getch();
                                }
Output------à Loop execution:1: times!
                                Loop execution:2: times!
 Loop execution:3: times!
Execution complete!

     (5)    Qualifier:- The C compiler provides some reserve words named: const,volatile,restrict that are known as qualifier is responsible to define a properties of variable at definition time.
(i)                  const:- When a variable preceded by const qualifier then it ensure that a value of variable can be used in application/program but it can’t be alter later on.
const datatype varname=value;
 
Syntax:- 
Example:-
(i)                  Write a program in C to accept a radius of circle from keyboard. calculate and display area of circle.
void main()
{
const float pi=3.14f;
float r;
printf(“Enter radius of circle:”);
scanf(“%f”,&r);
printf(“Area of circle=%f”, pi*r*r);
getch();
}
Output-------à Enter radius of circle=2.5
                                Area of circle=19.625
NOTE:- A value of constant variable can be changed by using inbuilt function scanf().
(ii)                void main()
{
const int a=5;
clrscr();
printf(“Value of a=%d”,a);
printf(“Enter a number:”);
scanf(“\n Value of a= %d”,a);
getch();
}
Output----à      Value of a=5
                                Enter a number=20
                                value of a=20


Comments

Popular Posts