C++ Storage Classes

The storage class is used to specify control two different properties: storage lifetime and scope(visibility) of variables.

Following storage classes can be used in a C++ Program:
  • Automatic
  • External
  • Static
  • Register

Automatic(auto) Storage Class

Variable defined within the function body are called auto variable. The auto storage class is used to declare automatic variables, which is also called local variables.

auto int a, b, c = 100;

is same as:

int a, b, c = 100;

The External Storage Class

External variables are defined outside of the function. Once External variable declared, the variable can be used in any line of codes throughout the rest of the program.

The extern modifier is most commonly used when there are two or more C++ files sharing the same global variables or functions.

First File : main.cpp

#include <iostream>
#include "file.cpp"
int count ;
extern void write_extern();
   count = 5;

Second File : file.cpp

#include <iostream>
extern int count;
void write_extern(void)
   std::cout << "Count is " << count << std::endl;
Program Output:


Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.