Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Static or Non-Static Variable: Which Type of Variable is suitable and when?

Understanding the differences Between Global Static and Global Non-Static VariablesC++.

Ayush Raj
Dev Genius
Published in
5 min readMar 13, 2023

Introduction

Operating systems and video games are only two examples of the numerous applications that may be created using the well-liked programming language C++. The capability of C++ to define and handle many sorts of variables is one of its key characteristics. Global, static, local, and non-static variables can all be defined in C++, and each kind has a different set of properties and scope.

The variations between these variable types and their scopes will be discussed in this article, along with how they impact a C++ program’s operation. To make the distinctions clearer, we’ll give instances of each sort of variable and contrast their results. You will know more about C++ variable scopes and types after reading this article.

Terminology:

In C++, variables can be declared with different scopes and storage classes, which determines their visibility and lifetime within a program. Let’s start with the textbook definitions of global, static, local, and non-static variables in C++:

  1. Global Variables: These are variables that are defined outside of any function or class. Global variables have a global scope and are visible throughout the program. They are created at the beginning of the program and remain in memory until the program terminates. Global variables can be accessed by any function or class within the program.
  2. Static Variables: These are variables that are declared with the “static” keyword. Static variables have a local scope but a lifetime that extends throughout the program. When a static variable is declared inside a function, it retains its value between function calls. When a static variable is declared outside of any function or class, it has a global scope and can be accessed by any function or class within the program file.
  3. Local Variables: These are variables that are declared inside a function or block of code. Local variables have a local scope and are only visible within the function or block where they are declared. They are created when the function or block is executed and are destroyed when the function or block exits.
  4. Non-static Variables: These are variables that are not declared with the “static” keyword. Non-static variables have a local scope and are only visible within the function or block where they are declared. They are created when the function or block is executed and are destroyed when the function or block exits. Non-static variables do not retain their value between function calls.

Understanding the difference by code:

Local static v/s local non-static variable

#include <iostream>
using namespace std;

void function()
{
int localNonStatic = 0;
static int localStatic = 0;

localNonStatic++;
localStatic++;

cout << "localNonStatic = " << localNonStatic << endl;
cout << "localStatic = " << localStatic << endl;
cout << "--------------"<< endl;
}

int main(){
cout << "Call to function(): " << endl;
function();
function();
function();
function();

return 0;
}
//filename:local3.cpp
>>g++ local3.cpp
>>./a.out
Calling the function
localNonStatic = 1
localStatic = 1
--------------
localNonStatic = 1
localStatic = 2
--------------
localNonStatic = 1
localStatic = 3
--------------
localNonStatic = 1
localStatic = 4
--------------

The output of this program shows the values of two variables declared inside a function: localNonStatic and localStatic.

localNonStatic is a non-static variable, which means it has a local scope and is created each time the function is called. In this program, localNonStatic is initialized to 0 at the beginning of each function call, and its value is incremented by 1 every time the function is executed. Therefore, the output shows that localNonStatic is always equal to 1, since it is reset to 0 at the beginning of each function call.

localStatic, on the other hand, is a static variable, which means it has a local scope but a lifetime that extends throughout the program. localStatic is initialized to 0 only the first time the function is called, and its value is incremented by 1 every time the function is executed. Since localStatic retains its value between function calls, the output shows that its value increases by 1 every time the function is executed. Therefore, the output shows that localStatic starts at 1 and increases by 1 each time the function is called.

In summary, the difference between localNonStatic and localStatic is that localNonStatic is reset to its initial value at the beginning of each function call, while localStatic retains its value between function calls.

Global static v/s global non-static variable

Global non-static variable

// File: global1.cpp
#include <iostream>
using namespace std;

//static int global_var = 42; // static global variable
int global_var = 42;
// File: global2.cpp
#include <iostream>
using namespace std;

extern int global_var;
int main() {
cout << "Global variable value from global2.cpp " << global_var << endl; // this will cause a linker error if static global
return 0;
}
>>g++ global2.cpp global1.cpp
>>./a.out
Global variable value from global2.cpp 42

Global static variable

// File: global1.cpp
#include <iostream>
using namespace std;

static int global_var = 42; // static global variable
//int global_var = 42;
// File: global2.cpp
#include <iostream>
using namespace std;

extern int global_var;
int main() {
cout << "Global variable value from global2.cpp " << global_var << endl; // this will cause a linker error if static global
return 0;
}
>>g++ global2.cpp global1.cpp
/nix/store/039g378vc3pc3dvi9dzdlrd0i4q93qwf-binutils-2.39/bin/ld: /tmp/ccbhNoCM.o: in function `main':
global2.cpp:(.text.startup+0x20): undefined reference to `global_var'
collect2: error: ld returned 1 exit status

In the first case, global_var is a non-static global variable, meaning it is accessible and modifiable by all functions in all files that include the header where the variable is declared. In global1.cpp, global_var is defined as 42, and in global2.cpp, it is accessed using extern int global_var, which declares that the variable is defined in another file. When the two files are compiled and linked together using g++ global2.cpp global1.cpp, the linker successfully links global_var and the program outputs the expected value of 42.

In the second case, global_var is a static global variable, meaning it is accessible and modifiable only within the file where it is declared. In global1.cpp, global_var is defined as 42, but with the keyword static. When global2.cpp attempts to access global_var using extern int global_var, it results in a linker error because the variable is only visible within global1.cpp due to its static storage class.

In a nutshell…

In conclusion, global variables have a global scope and can be accessed from anywhere in the program, while local variables have a local scope and can only be accessed within their block. Static variables retain their value even after their scope has ended, and non-static variables are created each time their scope is entered. Choosing between these types of variables depends on the specific needs of the program and the desired functionality. Ultimately, understanding the differences and appropriate usage of each type of variable is crucial for effective programming in C++.

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Responses (1)

Write a response