Static storage
- C
- C++
src/c/basics/07-static-storage.c
#include <stdio.h>
// variables declared in the file scope are static
int global_var = 0;
void increase_global_var()
{
global_var++;
printf("global var called %d times\n", global_var);
}
void increase_static_var()
{
// static variable are initialized to 0, and initialized once
static int static_var = 0;
static_var++;
printf("static var called %d times\n", static_var);
}
void increase_local_var()
{
int local_var = 0;
local_var++;
printf("local var called %d times\n", local_var);
}
int main()
{
increase_global_var();
increase_global_var();
increase_static_var();
increase_static_var();
increase_local_var();
increase_local_var();
return 0;
}
src/cpp/basics/07-static-storage.cpp
#include <iostream>
using namespace std;
// variables declared in the file scope are static by default
// and initialized to zero.
int no_of_calls;
void increase_count()
{
no_of_calls++;
cout << "The function is called " << no_of_calls << " time(s)" << endl;
}
void increase_count_static()
{
static int static_no_of_calls = 0;
// by default static objects are initialized to zero
// even if we initialize, it will be initialized only once
static_no_of_calls++;
cout << "The static function is called " << static_no_of_calls
<< " time(s)" << endl;
}
void increase_count_volatile()
{
int volatile_no_of_calls = 0;
volatile_no_of_calls++;
cout << "The volatile function is called " << volatile_no_of_calls
<< " time(s)" << endl;
}
int main()
{
increase_count();
increase_count();
cout << endl;
increase_count_static();
increase_count_static();
cout << endl;
increase_count_volatile();
increase_count_volatile();
return 0;
}