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.
The storage classes
of a variable tell the compiler:-
- The storage area of the variable
- The initial value of the variable if not initialized
- The scope of the variable
- 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:
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