ü Storage
Classes
Features of any
variables is defined by its storage class of that variable. Compiler stores the
variables and it is defined by the storage class that where the compiler will
store the variable, its initial value, scope and lifetime.
Storage classes in C:
-
·
auto
·
extern
·
static
·
register
Now, let’s have a
look on each of these storage classes closely.
· auto
Variables which belongs to ‘auto’ storage class
are defined within a function or a block. These variables are by default
assigned some garbage value and as they are local to the function, these
variables are also known as Local Variables. These variables can only be
accessed inside that function in which they are declared. While declaring these
variables there is no need to put ‘auto’ as these are by default auto.
Example:
#include <stdio.h>
#include<conio.h>
int sum(int n1, int n2)
{
auto int s; //declaration of auto (local) variable
s = n1+n2;
return s;
}
int main()
{
int i = 2, j = 3, k;
k = sum(i, j);
printf("sum is : %d\n", k);
return 0;
}
Output:
Here, The variable ‘s’ can only be used
inside the function as it is declared inside the function ‘sum’. Also, there is
no need to put auto while declaring it.
·
extern
Keyword extern
is written before a variable so that compiler can know that this variable was
declared somewhere else. Basically, ‘extern’ keyword is written before any
variable to tell us that this variable is global variable which is declared in
some other program file.
Now, let’s have a look on what actually happens.
We all know what global variable is. ‘Global
Variable’ is a declared outside of all the functions. Global variable can be
from anywhere in the program and its value can be changed anytime within any
function as follows:
#include <stdio.h>
#include <conio.h>
int g;
void print()
{
g = 10;
printf("g = %d\n", g);
}
int main()
{
g = 7;
printf("g = %d\n", g);
print();
return 0;
}
Output:
Here, we can see that ‘g’ is the global variable
which is defined outside of every functions. Its value was assigned 7 and in
the print function as 10.
When we declare global variable, some space is
allocated to it in memory like all other variables. A value can be assigned to
it in the same program in which it was declared as we did in the example. But
is it possible to use it or assign it a value in any other program file?
It can be done by using extern keyword as shown below.
firstfile.c
int g =
0;
We declared a global variable ‘g’ in
the first program file firstfile.c
Now, in a header file firstfile.h declare this variable ‘g’
and after that include it in the other file in which we want to use this
variable.
firstfile.h
extern
int g;
In order to use global variable ‘g’,
in the second program file secondfile.c,
we will have to include header file in it by writing #include “firstfile.h”. Here to the variable ‘g’ we assign a value
4 and thus in this program the value of ‘g’ becomes 4.
secondfile.c
#include "firstfile.h"
main(){
g = 4;
printf("%d", g);
}
main(){
g = 4;
printf("%d", g);
}
·
static
A variable declared as static once
initialized, exists till the end of the program. If a static variable is
declared inside a function, it remains into existence till the end of the
program and not get destroyed as the function exists (as in auto). If a static
variable is declared outside all the functions in a program, it can be used
only in the program in which it is declared and is not visible to other program
files(as in extern).
Let's see an example of a static
variable.
If the variable is declared as ‘static’ is
initialized, it exits till the end of the program. A static variable remains in
the program and not get destroyed as the function exists, if that variable is
declared inside a function.
If a static variable is declared outside of all
the functions in a program, it can be used only in the program in which it is
not visible to the other program files and in which it is declared.
Now, let’s have a look on an example of a static
variable.
#include <stdio.h>
#include<conio.h>
static int g = 5;
void fn()
{
static int i = 0;
printf("g = %d\t", g--);
printf("i = %d\n",i++);
}
int main()
{
while(g >= 2)
fn();
return 0;
}
Output:
Here, static variables are ‘g’ and 'i' in which 'i' is a local variable and ‘g’
is global variable. If before the declaration of 'i' we had not written
‘static’, then every time the function ‘fn()’ would have been called, every
time 'i' would have been declared with an initial value of 0 it would also have
got destroyed as the function ‘fn()’ would exists.
·
Register
Register let the compiler know that the variable will get
stored in a register instead of memory (RAM). A register variable can be
accessed faster than a normal variable.
Not all the registers defined as register
will get stored in a register as it depends on several restrictions of hardware
and implementation.
The address of such variables cannot be accessed since these
do not have a memory location as which is clear by the following example:
#include <stdio.h>
#include <conio.h>
int main()
{
register int n = 20;
int *ptr;
ptr = &n;
printf("address of n : %u", ptr);
return 0;
}
No comments:
Post a Comment