ADS

Tuesday, 16 February 2021

Storage Classes or Area or scope of Variables in C Programming

 Storage Classes or Area of scope of Variables in C Programming:

The area or block of the C programming from where the variables can be accessed is known as the scope of variable.

The scope of the variable depends on its storage class.
i.e where and how it is declared.

scope of Variables


The storage classes of a variable tell the compiler:-

  1. The storage area of the variable
  2. The initial value of the variable if not initialized
  3. The scope of the variable
  4. Life of the variable i.e how the long the variable would be active in the program.


Any variable declared in C can have any one of the four storage classes-

1-Automatic Variables (Local Variable):

a Keyword used for this is ‘auto’. Auto variables are declared inside the function without storage class name, by default is an auto variable. The scope of the variable is local to the block in which they are defined Once the execution of the function takes place. The function contents and existence of the auto variables or local variables vanish (finish), they cannot be accessed directly by other function.

   Ex: WAP to show the working of auto variables:

      #include<sdtio.h>

      #include<conio.h>

       void main()

       {

         int y=10;

         call2();

        print(“\ny=%d”.y);

        } 

        Void call1() 

       {

           int y=20;

           print(“\n y=%d”,y);

        }

      Call2()    

      {

      int y=30;   

     call1();

      print(“\n y=%d”,y);

      } 

     ---------------------

     output

     y=20

     y=30

     y=10


       Ex: 2 WAP to show working of auto variable in different blocks:

void main()

{

int x=10;

printf(“\n x=%d”,x);

{

int x=20;

printf(“\n x=%d”,x);

}

printf(“\n x=%d”,x);

}


            output:

x=10;
x=20
x=10

 2.External variables or Global Variable:

Extern keyword used for this variables. The variables that available to all the functions.
i.e. from entire program they can be accessed. These are called external or global variables. These are declared outside the function body.
 
In case both external and auto variables are declared with the same name, in a program the first priority is given to the auto variable.

      WAP to show working of external variables.
void call1();
Void call2();
Int v=10;
Void main()
{
call1();
Call2();
Printf(“\n in main() v=%d ”,v);
}
Void call1()
{
printf(“\n call1() v=%d’,v);
}
void call2()
{
Printf(“\n in call2() v=%d”,v);
}
---------------
Output:
in call1() v=10
in call2() v=10
in main() v=10



 3. Static variables:

The static variable may be of an external or internal type depending upon where it is declared. If declared outside the function of the body it will be static global. If declared in the body or block then it is auto.
When a variable is declared as static its garbage value is removed and initialized to null value the contents of this variables remains constant through the program execution. A static variable is initialized only once. It is never reinitialized. The value of the static variable persists at each call and the last change made is the value of static variable remains throughout the program execution.


Ex:

wap to show the difference between variables of auto and static class when they are not initialized.

void Main()

{

int x;

static int y;

Print(“\n x=%d & y=%d”,x,y);

}

______________________________

output:

x=1026 (garbage values)    &       y=0 



4. Register variables:

    we can also keep some variables in the CPU register instead of memory. The keyword register tells the compiler that the variables are in CPU register. If the CPU fails to keep the variables in CPU register in that case the variables are assumed as auto and stored in the memory. CPU register are limited in number hence we cannot declared more variable as register variable. However compiler automatically converts register variable to non register variables once the limit in full.

 

The CPU register in microcomputer are 16 bit register. The data type float and double needs space of more than 16 bits then the compiler treat them as auto variables.

 

Ex:

WAP to declare and use variable of register class

void main()

{

Register int m=1;

For(; m<=5;m++)
{

Printf(“\n %d”,m);
}



 -----------------------
output:
12345


No comments:

Post a comment

Popular Posts