C++ Scope & Lifetime
In this page:
Local Scope
A variable declared inside a function exists only within that function — it comes into being when the function starts and is destroyed the moment the function returns, making it invisible and inaccessible to any code outside that function. This local scope is what keeps different functions from accidentally interfering with each other's temporary data.
Example: Local Scope
#include <iostream>
void showLocal() {
int local = 5;
std::cout << local << std::endl;
}
int main() {
showLocal();
// std::cout << local; // error: local doesn't exist here
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Global Scope
A variable declared outside every function, at the top level of a file, has global scope and can be read or modified from anywhere in the program, persisting for the program's entire runtime. Global variables are convenient for truly shared state, but overusing them makes it hard to track which function changed a value and when.
Example: Global Scope
#include <iostream>
int globalCounter = 0;
void increment() {
globalCounter++;
}
int main() {
increment();
increment();
std::cout << globalCounter << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Block Scope
Any variable declared inside a pair of curly braces — whether that's an if block, a loop, or a standalone block — is confined to that block and ceases to exist once execution passes the closing brace. This is a stricter version of local scope, and it's what lets a loop variable named i be reused safely across several separate loops in the same function.
Example: Block Scope
#include <iostream>
int main() {
if (true) {
int blockVar = 42;
std::cout << blockVar << std::endl;
}
// std::cout << blockVar; // error: blockVar is out of scope here
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Name Hiding
If a local variable shares its name with a global variable, the local one takes priority everywhere inside its own scope, effectively hiding the global variable from view until that local scope ends. You can still explicitly access the hidden global using the scope resolution operator, ::variableName, if you genuinely need it.
Example: Name Hiding
#include <iostream>
int value = 100;
void showValue() {
int value = 5; // hides the global "value" inside this function
std::cout << value << std::endl;
}
int main() {
showValue();
std::cout << value << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Local Variables
Marking a local variable static changes its lifetime dramatically: instead of being destroyed and recreated on every function call, it's initialized only the first time the function runs and then retains its value across every subsequent call. This is useful for something like a call counter that needs to persist between invocations without being a global variable.
Example: Static Local Variables
#include <iostream>
void counter() {
static int count = 0;
count++;
std::cout << count << std::endl;
}
int main() {
counter();
counter();
counter();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions