C++ Part 35

Typical Program Organization

# include <iostream>  //Setup section used by compiler
storage class of a variable defines the lifetime and visibility of a variable.
Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible.
There are five types of storage classes in C++.
They are:
  1. Automatic
  2. External
  3. Static
  4. Register // Next Post
  5. Mutable // Next Post
______________________________

1. Automatic Storage Class

Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default.
This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.

Syntax of Automatic Storage Class Declaration

datatype var_name1 [= value];
or
auto datatype var_name1 [= value];

Example of Automatic Storage Class

auto int x;
float y = 5.67;

2. External Storage Class

External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.

Syntax of External Storage Class Declaration

extern datatype var_name1;
For example,
extern float var1;

Example of External Storage Class

Example 1: C++ program to create and use external storage.
File: sub.cpp
int test=100;  // assigning value to test

void multiply(int n)
{
    test=test*n;
}
File: main.cpp
#include<iostream>
#include "sub.cpp"  // includes the content of sub.cpp
using namespace std;

extern int test;  // declaring test

int main()
{
    cout<<test<<endl;
    multiply(5);
    cout<<test<<endl;
    return 0;
}
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100 in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.
Note: Run the main.cpp program
Output
100
500

3. Static Storage Class

Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.

Syntax of Static Storage Class Declaration

static datatype var_name1 [= value];
For example,
static int x = 101;
static float sum;

_________

source:  www.programtopia.net

Post a Comment

Popular Posts