Storage Classess in C++





To fully define a variable one needs to mention not only its 'type'’ but also its 'storage class'. In other words, not only do all variables have a data type, they also have a ‘storage class’.

There are basically two kinds of locations in a computer where such a value may be kept—Memory and CPU registers.It is the variable’s storage class that determines in which of these two locations the value is stored.

  • Storage class in C++ decides the part of storage to allocate memory for a variable, it also determines the scope of a variable.

  • All variables defined in a C++ program get some physical location in memory where variable's value is stored.

  • Memory(RAM) and CPU registers are types of memory locations where a variable's value can be stored.

Types of Storage classes :

  1. Automatic Storage Class.
  2. Static Storage Class.
  3. External Storage Class.
  4. Register Storage Class.


1. Automatic Storage Class:

A variable defined within a function or block with auto specifier belongs to automatic storage class.

All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned.

DETAILS

KEYWORD : auto
VISIBILITY : Local to the block
STORAGE : MEMORY
DEFAULT VALUE : Garbage
LIFE TIME : Till control remain in the block

EXAMPLE

#include< iostream >
void fun( );
int main( ) {
 fun();
 fun();
 fun();
 return 0;
}
void fun() {
 auto int x=0;
 x=x+1;
 cout<<"X ="<< x << endl;
}
OUTPUT:
X = 1
X = 1
X = 1

2. Static Storage Class:

When static specifier is applied to a local variable, the compiler creates permanent storage for it, much as it creates storage for a global variable but static local variable remains visible only to the function or block in which it is defined.

In simple terms, a static local variable is a local variable that retains its value between function calls.

DETAILS

KEYWORD : static
VISIBILITY : Local to the block
STORAGE : MEMORY
DEFAULT VALUE : Zero ( 0 )
LIFE TIME : Till the program is running.

EXAMPLE

#include< iostream >
void fun( );
int main( ) {
 fun();
 fun();
 fun();
 return 0;
}
void fun() {
 static int x=0;
 x=x+1;
 cout<<"X = "<< x<< endl;
}
OUTPUT:
X = 1
X = 2
X = 3

3. External Storage Class:

The extern specifier gives the declared variable external storage class.

The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program.

DETAILS
KEYWORD : extern
VISIBILITY : Global
STORAGE : MEMORY
DEFAULT VALUE : Zero ( 0 )
LIFE TIME : Till the program is running.

EXAMPLE

#include< iostream >
int main( ) {
 extern int x;
 cout<<"X = "<< x; 
 return 0;
}
int x = 40;
OUTPUT:
X = 40

4. Register Storage Class:

A register declaration is equivalent to an auto declaration, but hints that the declared variable will be accessed frequently; therefore they are placed in CPU registers, not in memory.

DETAILS
KEYWORD : register
VISIBILITY : Local to the block
STORAGE : CPU's Register
DEFAULT VALUE : Garbage
LIFE TIME : Till control remain in the block

EXAMPLE

#include< iostream >
int main( ) {
 register int x = 10;
 int sum = x+x+x+x+x;
 cout<<"X ="<< x; 
 return 0;
}
OUTPUT:
X = 50

Next topic is Environment Setup





 




Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved