Variable Storage Class Specifier in CPP

  1. Home
  2. Tutorials
  3. CPP
  4. CPP Programs
  5. Variables
  6. Program

Source Code

#include <iostream>

using namespace std;

int g;  // declaration of global variable.

int main(int argc, char** argv) {
 

    // declaration of local variable.
    auto int l;  // auto keyword is optional for local variables.
    
    for ( register int i = 1 ; i <= 5 ; i++) {
        cout<<"Register Variables are fast in execution"<<endl;
    }
    
    // declaration of static variables.
    static int s;

    return 0;
}

Output

storage class specifier in c++Working

This program demonstrate declaration and usage of variable according to storage class specifier. Here we have declare these four types of variables.

  1. global

  2. local

  3. static

  4. register

One global variable of name g is declared outside the main function. We can access this variable anywhere in the program. Means we can set or get it's value. Similary variables of type auto (local), static and register are declared. These all variables are of type int. Register variables are created in register memory that's why these variables are much faster in access. These variables are normally declared for iterations.

One static variable is declared of name s. Static variables are similar to gloabl variable for lifetime and similar to local variable for accessibility.

Comments
Login to TRACK of Comments.