What is the difference between static
global and non-static
global identifier in C++?
The difference between a static variable and a global variable lies in their scope. A global variable can be accessed from anywhere inside the program while a static variable only has a block scope.
A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once. A non-static variable may occupy more space.
A global static variable is one that can only be accessed in the file where it is created. This variable is said to have file scope. Constant Variables. In C, the preprocessor directive #define was used to create a variable with a constant value. This still works in C++, but problems could arise.
Static limits the scope of the variable to the same translation unit.
A static global variable has internal linkage.
A non-static global variable has external linkage.
Good Read:
What is external linkage and internal linkage?
A global static
variable is only available in the translation unit (i.e. source file) the variable is in. A non-static global variable can be referenced from other source files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With