The scope of variables means that if a variable is defined in a C++ program, it determines in which parts of the same program this variable can be accessed.
There are two types of variable scopes:
- Local Variables
- Global Variables
1. Local Variables
Variables declared inside a specific part of a program, like a function or block of code, are known as local variables. These variables can only be used within that particular block and cannot be accessed from outside of it. In simpler terms, their “scope” is limited to that block, and they stay hidden from the rest of the program.
For example, in the following code, the variable x
is a local variable, and it can’t be accessed outside the boundary of the nested block.
#include <iostream> using namespace std; int main() { { int x = 10; // This is a local variable within a nested block cout << "Inside the nested block: " << x << endl; } // Uncommenting the line below will result in a compilation error // cout << "Outside the nested block: " << x << endl; return 0; }
In this example, x
is declared as a local variable inside the nested block, and it cannot be accessed outside of that block. Uncommenting the line trying to access x
outside the block would result in a compilation error because x’s scope is limited to the nested block.
A C++ Program That Demonstrates The Use of Local Variables
#include <iostream> using namespace std; void myFunction() { int localVar = 24; //Local variable } int main() { myFunction(); // Call the function // Attempt to access localVar here (will result in a compilation error) cout << localVar << endl; // This line will cause a compilation error return 0; }
OUTPUT OF THE PROGRAM
[Error] 'localVar' was not declared in this scope
In the above program,
- We have a function called
myFunction
, where we create a variable calledlocalVar
. localVar
is a local variable, which means it exists only within themyFunction
function.- When we try to access
localVar
in the main function (outside of myFunction), it causes a compilation error becauselocalVar
is not visible or accessible outside of its own function.
2. Global Variables
Global variables in C++ are the ones you declare at the start of your program before the main() function. These global variables are visible and accessible from any part of your code.
Global variables in C++ can be accessed from both inside and outside of all the functions within the same program. To declare global variables, you simply define them outside of any function or block directly in the program’s body.
For example, in the following code, the variable globalVar
is a global variable, and it can be accessed from any point within the same program.
#include <iostream> using namespace std; // This is a global variable accessible throughout the program int globalVar = 20; // Function prototype declaration void anotherFunction(); int main() { // Accessing the global variable from within the main function cout << "Accessing globalVar from inside the main function: " << globalVar << "\n"; // Call anotherFunction to access globalVar anotherFunction(); return 0; } // You can access globalVar from outside any function void anotherFunction() { // Accessing the global variable from within anotherFunction cout << "Accessing globalVar from inside anotherFunction: " << globalVar << "\n"; }
In the main
function, we print the value of globalVar
, and then we call anotherFunction
, which also accesses and prints the same global variable.
A C++ Program That Demonstrates The Use of Global Variables
#include <iostream> using namespace std; // This is a global variable int globalVar = 24; void myFunction() { // Attempt to access the global variable inside the function cout << "Inside myFunction: globalVar = " << globalVar << endl; } int main() { // Access the global variable from the main function cout << "Inside the main function: globalVar = " << globalVar << endl; myFunction(); // Call the function return 0; }
OUTPUT OF THE PROGRAM
Inside the main function: globalVar = 24
Inside myFunction: globalVar = 24
In the above program,
- We declare a global variable named
globalVar
outside of any function, making it accessible throughout the entire program. - Inside the main() function, we access and print the value of
globalVar
. - We call the myFunction function, which also attempts to access and print the value of
globalVar
.
When you run this program, you’ll see that the global variable globalVar
can be accessed from both the main function and myFunction
, demonstrating its global scope.
Local Variables vs. Global Variables
Local Variables | Global Variables | |
---|---|---|
Scope | Limited to the function where declared | Accessible from any part of the program |
Value | Contains garbage value if not initialized | Contains zero or default value if not initialized |
Memory Storage | Stored on the stack (unless specified) | Stored in a fixed location decided by the compiler |
Accessed | Accessible only inside the declaring function | Accessible globally within the program |
Lifetime | Created when the function is called, ends when the function exits | Created at the start of the program, ends when the program terminates |
Data Sharing | Cannot be shared between functions | Shared across multiple functions |
Parameters | Requires parameter passing to share data between functions | Accessible without explicit parameter passing |