Examples of storage classes in c++ programing

0

storage class controls two different properties of a variable: lifetime and scope

Examples of storage classes :int, float, char etc. 

All Contributions

Extern function example :

#include <iostream>

using namespace std;
// declaring the variable which is to
// be made extern an initial value can
// also be initialized to x
int x;
void externStorageClass()
{
    cout << "Demonstrating extern class\n";
    // telling the compiler that the variable
    // x is an extern variable and has been
    // defined elsewhere (above the main
    // function)
    extern int x;
    // printing the extern variables 'x'
   cout << "Value of the variable 'x'"<< "declared, as extern: " << x << "\n";
    // value of extern variable x modified
    x = 2;
    // printing the modified values of
    // extern variables 'x'
    cout<< "Modified value of the variable 'x'"<< " declared as extern: \n"<< x;
}
int main()
{
     // To demonstrate extern Storage Class
    externStorageClass();

    return 0;
}

Extern function example :

#include <iostream>

using namespace std;
// declaring the variable which is to
// be made extern an initial value can
// also be initialized to x
int x;
void externStorageClass()
{
    cout << "Demonstrating extern class\n";
    // telling the compiler that the variable
    // x is an extern variable and has been
    // defined elsewhere (above the main
    // function)
    extern int x;
    // printing the extern variables 'x'
   cout << "Value of the variable 'x'"<< "declared, as extern: " << x << "\n";
    // value of extern variable x modified
    x = 2;
    // printing the modified values of
    // extern variables 'x'
    cout << "Modified value of the variable 'x'"<< " declared as extern: \n"<< x;
}

int main()
{
     // To demonstrate extern Storage Class
    externStorageClass();
    return 0;
}

Auto storage class example

#include <iostream>
using namespace std;
void autoStorageClass()
{
   cout << "Demonstrating auto class\n";
    // Declaring an auto variable
    // No data-type declaration needed
    auto a = 32;
    auto b = 3.2;
    auto c = "example geek";
    auto d = 'G';
    // printing the auto variable
    cout << a << " \n";
    cout << b << " \n";
    cout << c << " \n";
    cout << d << " \n";
}
int main()
{
    // To demonstrate auto Storage Class
   autoStorageClass();
    return 0 ;
} 

Local variable example :

#include <iostream>
using namespace std;
void test();
int main() 
{
    // local variable to main()
    int var = 5;
    test();    
    // illegal: var1 not declared inside main()
    var1 = 9;
}
void test()
{
    // local variable to test()
    int var1;
    var1 = 6;
    // illegal: var not declared inside test()
    cout << var;
    return 0 ;
}

Local variable example :

#include <iostream>
using namespace std;
void test();
int main() 
{
    // local variable to main()
    int var = 5;
    test();    
    // illegal: var1 not declared inside main()
    var1 = 9;
}
void test()
{
    // local variable to test()
    int var1;
    var1 = 6;
    // illegal: var not declared inside test()
    cout << var;
    return 0 ;
}

total contributions (5)

New to examplegeek.com?

Join us