Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between static global and non-static global identifier in C++?

What is the difference between static global and non-static global identifier in C++?

like image 702
Fahad Siddiqui Avatar asked Oct 31 '12 16:10

Fahad Siddiqui


People also ask

What is the difference between static and global variables 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.

What is difference between static and non static?

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.

What is static global in C?

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.


2 Answers

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?

like image 177
Alok Save Avatar answered Oct 11 '22 02:10

Alok Save


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.

like image 40
Some programmer dude Avatar answered Oct 11 '22 02:10

Some programmer dude