Wednesday, March 28, 2018

Write a C++ program to illustrate storage classes – static


AIM: Write a C++ program to illustrate storage classes –  static

      THEORY:

Static is a storage classes for all global variables. It have a life time over the entire program
i.e., memory  for the static variables is allocated when the program begins running and is freed
when the program terminates . To declare an integer X as static variable,
                  static int X=10;
Here, X is an global variable . Static local variables when defined within a function are initialized at the runtime . Hence , the static variable inside a function retains its values during various calls.

SOURCE CODE:

#include<iostream>
using namespace std;
void func(void);
static int count=10;
int main()
{
   while(count--)
    {
            func();
    }
            return 0;
}
void func(void)
{
   static int i=5;
   i++;
   cout<<"i is:"<<i;
   cout<<"and count is:"<<count<<endl;
}


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home