← Back to C++ Course | Chapter 5: Functions | Lesson 12 of 13

C++ Scope & Lifetime

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

cpp
#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;
}

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

cpp
#include <iostream>

int globalCounter = 0;

void increment() {
	globalCounter++;
}

int main() {
	increment();
	increment();
	std::cout << globalCounter << std::endl;
	return 0;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#include <iostream>

void counter() {
	static int count = 0;
	count++;
	std::cout << count << std::endl;
}

int main() {
	counter();
	counter();
	counter();
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.