codescracker


c++

C++ Storage Classes



« Previous Tutorial Next Tutorial »


Storage classes in C++, basically defines the scope and life time of the variable, functions in your program. Here are the list of C++ storage classes available in C++:

Let's discuss about these C++ storage classes in detail one by one.

C++ auto Storage Class

C++ auto storage class is the default storage class for all the local variables in a C++ program. Here is the general form to use auto storage class in C++:

auto data_type variable_name;

Here, auto is the keyword used in declaring auto storage class variable, data_type is any valid C++ data type, and then variable_name is the name of the variable, which will be of auto storage class type. Here is an example

auto int counter;

Or, if you simply define any variable without preceding any storage class of C++, then that variable will be called as a variable of auto storage class. Like, here both the variable will be of auto storage class.

int a;
auto int b;

The above code fragment, defined two variables of same storage class which is auto.

Note - The auto can only be used within functions, or local variables.

Let's take an example program, demonstrating the auto storage class in C++

/* C++ Storage Classes - The auto Storage Class */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	auto int counter;
	int a;
	auto int b;
	cout<<"Enter any two number: ";
	cin>>a>>b;
	cout<<"\nPrinting these two number, 10 times:\n";
	for(counter=0; counter<10; counter++)
	{
		cout<<a<<"\t"<<b<<"\n";
	}
	getch();
}

Here is the sample output of the above C++ program:

c++ storage classes

As already told, that the auto can only be used within functions, i.e., local variables. Therefore in case, if you want to use auto on global variables like in this program, which is the wrong version of the above C++ program

/* C++ Storage Classes - Wrong use of auto Storage Class */

#include<iostream.h>
#include<conio.h>

auto int counter;      // error occurred..!!
auto int b;		// error occurred..!!

void main()
{
	clrscr();
	int a;
	cout<<"Enter any two number: ";
	cin>>a>>b;
	cout<<"\nPrinting these two number, 10 times:\n";
	for(counter=0; counter<10; counter++)
	{
		cout<<a<<"\t"<<b<<"\n";
	}
	getch();
}

If you try to compile or run the above C++ program, then this program will produce two error message as shown in the following figure:

c++ storage class

Now, let's discuss about register storage class of C++

C++ register Storage Class

The register storage class is also used to define the local variables. The register variables is stored in the register instead of RAM. Due to this, you can not define register variable which occupy more size than the maximum size of register. Because the register variable stored inside the register instead of RAM, so you can't apply & operator (address operator) on the register variable, since the & operator is used to acquire the address of the variable. Here is an example to declare register variable in C++

register int counter;

Here register is the keyword used in defining the register variables, int is the C++ data type, and counter is the variable name, which is stored in the register. Basically, register variable is used to provide quick and fast access. Therefore, register variable is mostly used in the loops. Let's take an example program, demonstrating the register variable in C++

/* C++ Storage Classes - The register Storage Class */

#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();
	int num1, num2;
	cout<<"Enter any two number: ";
	cin>>num1>>num2;
	cout<<"\nIncrementing and printing these two numbers 20 times:\n";
	for(register int counter=0; counter<20; counter++)
	{
		cout<<num1<<"\t"<<num2<<"\n";
		num1++;
		num2++;
	}
	getch();
}</pre>
		

Here is the sample run of the above C++ program:

storage classes in c++

Note - Defining register does not mean that the variable will be stored in a register. This means that it might be stored in a register, depending upon the hardware and the implementation restrictions.

C++ static Storage Class

C++ static storage class plays an important role in a C++ program, when the program require to keep previous value of a variable. Here is an example, uses static storage class in C++:

static int val = 10;

If the above statement, placed in any function like this

void fun(void)
{
	static int val = 10;
	val++;
	cout<<val<<" ";
}

And if you call this function 10 times, then this function will prints the value of variable val, which will be 11 12 13...20. In case, if you define the variable val, as simply int instead of static int, then value, 11 will be printed 20 times. Here is an example program, demonstrating the concept of static storage class of C++

/* C++ Storage Classes - The static Storage Class */

#include<iostream.h>
#include<conio.h>

void fun(void);
void main()
{
	clrscr();
	for(int i=0; i<10; i++)
	{
		fun();
	}
	getch();
}

void fun(void)
{
	static int val = 10;
	val++;
	cout<<val<<" ";
}

Here is the sample run of the above C++ program:

c++ auto storage class

As you can see from the above program, the static variable, val keeps their previous value. Now here is the program, demonstrating what happens if you remove static.

/* C++ Storage Classes */

#include<iostream.h>
#include<conio.h>

void fun(void);
void main()
{
	clrscr();
	for(int i=0; i<10; i++)
	{
		fun();
	}
	getch();
}

void fun(void)
{
	int val = 10;
	val++;
	cout<<val<<" ";
}

Here is the sample run of this C++ program:

c++ register storage class

C++ extern Storage Class

C++ extern storage class is simply used to give a reference of global variable. You can use extern storage class to define variables, functions, that you want to use these variables and functions inside another file. Here is an example:

/* C++ Storage Classes - The extern Storage Class */

#include<iostream.h>
#include<conio.h>

extern void welcome_message(void);

void main()
{
	clrscr();
	welcome_message();
	int num1, num2;
	cout<<"Enter any two number: ";
	cin>>num1>>num2;
	cout<<"\nThe sum of the two number is "<<num1+num2;
	getch();
}

void welcome_message(void)
{
	cout<<"Welcome to codescracker.com\n";
	cout<<"This is C++ Tutorial\n\n";
}

Here is the sample run of the above C++ program:

c++ static storage class

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test