- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Storage Classes
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++:
- auto
- extern
- register
- static
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:

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:

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(); }
Here is the sample run of the above C++ program:

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:

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++ 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:

« Previous Tutorial Next Tutorial »