c++ storage class

C++ Storage Class

In this article, you will learn the storage class in c++ and learn about the local variables, global variables, static local variables, and register variables.

Every variable in the cpp has the two property 
  1. datatype 
  2. storage class
The data type is int ,float and char types of data available in the cpp language.
The storage class tells about the scope and lifetime of the variable or function in the CPP language.


What is the storage class?

The storage classes are divide into 4 major parts of the variables.

local variable.
global variable.
static variable.
Register variable


C++ Local variable

These variables which present under function or anywhere inside the braces ({}) are the local variable or automatic variable.

Scope:
The scope of the local variables only inside the brackets({}) or where variables defined in the function.
If the variable declares inside the specific brackets({}) then the access the variable limited inside the brackets.

If the variable declares inside the function then the access of the variable is available inside this function.

Lifetime:
The life of the local variable is ended with function is exist or in the other words, a life of the variables ended when their scope ended.



#include <iostream>
using namespace std;

void testfunction();

int main() 
{
    
    int var = 15;//;local variable
cout<<var<<endl;

    testfunction();//calling the function
    
    //cout<<var1<<endl;
    
    //remove the above comment and then check what happened
}

void testfunction()
{

    int var1; //local variable
   
    var1 = 7;
    
    
}


C++ Global variable


These variables which are declared outside all the function in the program are called global variables.
Scope:
The scope the global variable is in the whole program,it can use anywhere in the program, in any function of the program.

Lifetime:
The lifetime of the global variable is very large as compared to the other variables.The life pf global variable ended as the program ended/

For Example:
#include <iostream>
using namespace std;

void testfunction();
int var = 15;  //declare the global variable    
int main() 
{
    
    
cout<<var<<endl;//accessing the varibale var in the main function

    testfunction();
    
   
}

void testfunction()
{

    cout<<var;//accessing the varibale var in the function
    
    
}

C++ Register  Variable


Basically, register variable use to make the program faster. Register variable stores on the register of the processor rather than store in the memory of the computer.It is easy and much faster to access the variable from the processor as compared to the memory.
register keyword is used for declaring the register variables.


Scope:
The scope of the register variable similar to a local variable, only access in the specific function where variables declared.

Life Time:
The lifetime of the register variables also similar to the local variables.


C++ Static local variable


Static variables declare by using the static keyword before the variable.

life time:
The static local variable similar to the local variable but the only difference in the lifetime.The lifetime of the static variable start when a function calls the first time and ends up when the program exists.

Scope:
The scope of the static variable is similar to the scope of the local variable.

For Example:
#include <iostream>
using namespace std;

void testfunction();
int var = 15;              //declare the global variable    
int main() 
{
    
    


    testfunction();            //calling the function
    testfunction();            //calling  the same function
    
   
}

void testfunction()
{
static int var =0;

var++;
    cout<< var <<endl;        //accessing the varibale var in the function
    
    
}
In the above code,static variable use in the function and increment in the value of the variable.The property of the static variable retain the value when the function call for the next time.Thats why,the value of var is 2 when function call second time.

Comments

Popular posts from this blog

Flutter layout

Diferrence between flutter and react native

how to cout in cpp