In c++ there are Two types of Storage classes. Those are
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;
int j=41;
j++;
i++;
cout<<“i is: “<<i<<”, j is: “<<j;
cout<<” and count is:”<<count<<endl;
}
OUTPUT:
i is 6, j is: 42 and count is 9
i is 7, j is: 42 and count is 8
i is 8, j is: 42 and count is 7
i is 9, j is: 42 and count is 6
i is 10, j is: 42 and count is 5
i is 11, j is: 42 and count is 4
i is 12, j is: 42 and count is 3
i is 13, j is: 42 and count is 2
i is 14, j is: 42 and count is 1
i is 15, j is: 42 and count is 0
Note: in above output ‘I’ initialized as 5 with static so it didn’t reinitialize again and ‘j’ is reinitialized every time in function call because ‘j’ is non-static variable.
AIM: Write a C++ program to illustrate storage classes-EXTERN
THEORY:
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’, the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.
The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
First File: main.c
#include<stdio.h>
int count;
extern void write_extern();
main(){
count = 5;
write_extern();
}
Second File: support.c
#include<stdio.h>
extern int count;
void write_extern(void){
printf(“count is %d\n”, count);
}
Here, extern is being used to declare count in the second file, where as it has its definition in the first file, main.c. Now, compile these two files as follows –
$gcc main.c support.c
It will produce the executable program a.out. When this program is executed, it produces the following result –
Count is 5
SOURCE CODE:
#include<iostream>
#include”exe2.cpp”
using namespace std;
int count;
extern void write_exteren();
int main()
{
count=5;
write_extern();
return 0;
}
exe2.cpp-
#include<iostream>
using namespace std;
extern int count;
extern void write_extern(void)
{
cout<<“count is: “<<count<<endl;
}
Output :
Count is: 5