A storage class defines the scope (visibility) and the life time of the variable in the program or in the class.
There are following storage classes:
-
auto
-
register
-
static
-
extern
The auto Storage Class
It is the default storage class for all the variables.
{
int mount;
auto int month;
}
The register Storage Class
The register storage class store the variable into the registers not in the RAM.
You cannot apply unary operator into it.
{
register int miles;
}
The static Storage Class
The static storage class instructs the compiler to keep the variable into the memory until program executes.
#include <iostream>
// Function declaration
void func(void);
static int count = 10; /* Global variable */
main()
{
while(count--)
{
func();
}
return 0;
}
// Function definition
void func( void )
{
static int i = 5; // local static variable
i++;
std::cout << "i is " << i ;
std::cout << " and count is " << count << std::endl;
}
The extern Storage Class
The extern storage class is used to defined a reference of a global variable accessible by every class.
First Program
#include <iostream>
int count ;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
Second Program
#include <iostream>
extern int count;
void write_extern(void)
{
std::cout << "Count is " << count << std::endl;
}
0 Comment(s)