Static Variables in C++

Static Variables in C++

In the C++ programming language, a user-defined static variable is a type of constant program variable that retains its static variable function value during a function call or when used or accessed multiple times within the current program. However, the scope or behavior of a static parameter is restricted to the defined function, block, or class in which the static parameter is declared or defined. Static parameters declared in a function can be either locally (declared within an existing function or block) or globally (declared outside the main function of an existing function or class) managed and accessed universally. A special unique feature of static variables is that they retain their value throughout the program’s execution life.

Static Variables in C++

Special Functions and Features of Static Variables in the C++ Programming.

  • Persistence of value – Unlike regular local variable declarations in a C++ program, static variables are automatically reinitialized every time an existing function is called. Remember, a static parameter variable declared in a C++ program maintains its value constant across function calls.
  • Scope – The use purpose or scope of a static variable declared in a C++ program remains restricted to the declaring block or function in which that static parameter is declared. You can never access the static variable from outside that scope.
  • Lifetime – The lifetime of a user-defined declared static variable persists throughout the program’s execution. It is created when a function starts the program and automatically terminates when the program ends.
  • Memory location – Static variables declared in a function program are stored and processed in the data segment of memory (in a specially ordered static memory area). There is no stack data storage method (for local static variables) or on the heap.
  • Initialization – Remember, a static variable declared in a function program is initialized only once in the program, and if it is not initialized explicitly in a function, it is automatically initialized to zero. Generally, a static variable (for basic types like int, float, etc.) can be defined in a data type method.

Syntax of a static variable in C++.

static return_type variable_name;

Element of a static local variable.

  • A static variable is declared or defined inside a function or block structure.
  • A user-defined static variable retains its default value between function calls.

Static global variable concept in C++.

Static global variables are declared outside of a function in a user-defined function program; you can declare them with the static reserved C++ keyword.

Global static variables declared in a program are accessible only within the same file; that is, they have internal linkage.

static return_type variable_name;

static local variable concept in C++.

Any static local variable declared in a C++ program retains its value across multiple user-defined function calls.

Example of a static local variable in C++.

#include <iostream>

using namespace std;

void testerUpdate() {

static int tester = 0; // Here we declare the tester static variable, which is initialized only once

tester++; // Here the tester static variable increments the value

cout << “Tester Update Function execute ” << tester << ” step.” << endl;

}

int main() {

testerUpdate(); // Result – Tester Update Function execute step 1.

testerUpdate(); // Result – Tester Update Function execute step 2.

testerUpdate(); // Result – Tester Update Function execute step 3.

testerUpdate(); // Result – Tester Update Function execute step 4.

return 0;

}

static local variable explanation in C++.

  • In this example, the static variable tester is initialized only once, and the output is displayed when the function testerUpdate() is called for the first time. On subsequent calls to testerUpdate() , the variable tester retains its value, and continues from where it previously stopped.

Static global variable concept in C++.

A static global variable declared in a C++ program can only be accessed and managed within the file in which it is first declared or defined. The file scope of the static global variable is pre-defined. This restricts the visibility of the static global variable to the file itself, making it inaccessible from other files in a multi-file program.

Example of a static global variable in C++.

#include <iostream>

using namespace std;

static int globalParameter = 1; // here we declare a static global variable with value

void preview() {

cout << “Static global variable value is – ” << globalParameter << endl;

}

int main() {

preview(); // Result – Static global variable value is – 1

globalParameter++; // Here we increment or modify the static global variable value

preview(); // Result – Static global variable value is – 2

return 0;

}

static global variable explanation in C++.

  • In this example, the static global variable globalParameter maintains its value across multiple function calls within the file. It cannot be accessed from other files, even if you have a static variable with the same name globalParameter defined in another file.

Combined example of local and global static variables in C++.

#include <iostream>

using namespace std;

// Here we declare a global static variable

int globalParameter = 21;

vo testLocalnGlobalVariables()

{

// Here we define a local variable

int localParameter = 9;

// Here we define a static local variable

static int staticLocalParameter = 0;

staticLocalParameter++;

cout << “\n################” << endl;

cout << “Static Local Variable setp – ” << staticLocalParameter << endl;

cout << “Static Global Variable value is – ” << globalParameter << endl;

cout << “Local Variable value is – ” << localParameter << endl;

cout << “#################” << endl;

}

int main()

{

cout << “\n ! First function call step !” <<endl;

testLocalnGlobalVariables();

cout << “\n ! Second function call step !” <<endl;

testLocalnGlobalVariables();

cout << “\n! Third function call step !” <<endl;

testLocalnGlobalVariables();

cout << “\n! Fourth function call step !” <<endl;

testLocalnGlobalVariables();

return 0;

}

Leave a Reply