. Advertisement .
..3..
. Advertisement .
..4..
Like most programming languages, C++ has its own rules when it comes to how to access variables declared in different places. You will need to have a great understanding of global variables in C++ to make use of them and avoid common problems. This article will help you with that.
Scope In C++
You can understand the scope of a variable as the extent of a program within which it is visible, and you can access or work with it. Other elements like functions and classes also have their scope, but beginners should be mainly concerned about the scope of variables.
When a variable is declared within a specific scope, you can have other variables of the same name in other parts of the program, as long as you don’t violate C++’s One Definition Rule (ODR).
Global Variables In C++
There are 6 types of scope in C++, including local and global scope. When you declare a name within a lambda or function, it has a local scope.
Variables of this scope are usually called “locals.” You can’t access them outside of the block where they have been declared.
#include <stdio.h>
#include <cstdlib>
void test();
int main () {
int g = 10;
printf("Local variable - main(): %d \n", g);
test();
return 0;
}
void test() {
printf("Local variable - test(): %d \n", g);
}
The compilation process will give you this error message:
main.cpp: In function ‘void test()’:
main.cpp:14:46: error: ‘g’ was not declared in this scope
14 printf("Local variable - test(): %d \n", g);
^
It happens because the test() function doesn’t have the right to access the g variable, which belongs to only main().
On the other hand, a name declared outside of any namespace, function, or class automatically has global scope. You can access it at any point from the place you declare it to the end of your code file.
#include <stdio.h>
#include <cstdlib>
int g = 10;
void test();
int main () {
printf("Global variable - main(): %d \n", g);
test();
return 0;
}
void test() {
printf("Global variable - test(): %d \n", g);
}
Output:
Global variable - main(): 10
Global variable - test(): 10
You can see that the global variable g can be accessed by both main() and test(). With external linkage, other files can see and use it as well – a practice called forward declaration.
a.cpp
int g = 10;
int main () {
return 0;
}
main.cpp
#include <stdio.h>
#include <cstdlib>
extern int g;
int main () {
printf("Global variable: %d \n", g);
return 0;
}
Output:
Global variable: 10
The extern keyword tells the compiler that the g variable exists somewhere in the source files. The job of the linker is to resolve all of the references to this variable.
Whenever possible, prefer locals over global variables, which need more instructions to access. If there are two variables (one local and one global) of the same name, the local variable will have precedence.
#include <stdio.h>
#include <cstdlib>
int g = 10;
int main () {
int g = 2;
printf("Value of g: %d \n", g);
return 0;
}
Output:
Value of g: 2
Conclusion
Global variables in C++ are declared outside of any function or class. As tempting as they are, you should resist the urge to use non-constant global variables or at least be careful with them.
Leave a comment