C++ Scope
The scope of a name is the region of code where that name can be used. Scope keeps programs manageable: a variable named i in one function has nothing to do with an i in another. C++ has several kinds of scope.
Local (block) scope
A variable declared inside a block { ... } exists from its declaration until the closing brace. Outside the block it is unknown:
#include <iostream>
using namespace std;
void demo() {
int local = 10; // visible only inside demo()
cout << "inside demo: " << local << "\n";
}
int main() {
demo();
for (int i = 0; i < 2; i++) {
int doubled = i * 2; // new variable each iteration
cout << "doubled: " << doubled << "\n";
}
// cout << local; // error: 'local' was not declared in this scope
// cout << i; // error: 'i' only exists inside the loop
}inside demo: 10
doubled: 0
doubled: 2
Function parameters also have local scope — they belong to the function body.
Global scope
A variable declared outside of all functions is global: every function in the file can see it.
#include <iostream>
using namespace std;
int requestCount = 0; // global
void handleRequest() {
requestCount++;
}
int main() {
handleRequest();
handleRequest();
handleRequest();
cout << "Requests handled: " << requestCount << "\n";
}Requests handled: 3
Warning: Global variables are convenient but dangerous: any function can change them, so bugs are hard to trace. Prefer passing values as parameters. Global constants (
const/constexpr) are fine.
Shadowing
An inner scope may declare a name that already exists in an outer scope. The inner one shadows (hides) the outer one until the block ends. The scope resolution operator :: with nothing in front of it reaches the global name:
#include <iostream>
using namespace std;
int value = 1; // global
int main() {
int value = 2; // shadows the global
{
int value = 3; // shadows the local one
cout << "innermost: " << value << "\n";
}
cout << "local: " << value << "\n";
cout << "global: " << ::value << "\n";
}innermost: 3
local: 2
global: 1
Shadowing is legal but confusing — compile with -Wshadow to get warnings about it.
Static local variables
A local variable marked static is created only once and keeps its value between calls, while still being visible only inside its function:
#include <iostream>
using namespace std;
int nextTicket() {
static int counter = 100; // initialised only on the first call
return ++counter;
}
int main() {
cout << nextTicket() << "\n";
cout << nextTicket() << "\n";
cout << nextTicket() << "\n";
}101
102
103
Namespace scope
A namespace groups related names and prevents clashes between libraries. Access its members with :::
#include <iostream>
using namespace std;
namespace physics {
const double g = 9.81;
double weight(double mass) { return mass * g; }
}
namespace game {
const double g = 20.0; // different 'g', no clash
}
int main() {
cout << physics::weight(10) << "\n";
cout << game::g << "\n";
}98.1
20
Summary
| Scope | Declared | Lifetime | Visible |
|---|---|---|---|
| Block / local | inside { } |
until the block ends | inside the block |
| Static local | inside a function, with static |
whole program | inside the function |
| Global | outside all functions | whole program | the whole file (and others with extern) |
| Namespace | inside namespace x { } |
whole program | via x::name |
Test yourself
Where can a variable declared inside a for-loop header be used?
- Anywhere in the function
- Only inside the loop
- Only after the loop
- Anywhere in the file
What is special about a static local variable?
- It is visible to all functions
- It keeps its value between calls
- It cannot be modified
- It lives on the heap
How do you refer to a global x when a local x shadows it?
- global.x
- this->x
- ::x
- extern x