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

C++ Scope & Lifetime

Scope is where a name can be seen, like a secret you can whisper only in one room. Names made inside a block vanish when the block ends.
Syntax
cpp
type global_name = value;  // global scope

void function_name() {
  type local_name = value;  // local scope, exists only in this function
}

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 std::cout and std::cin
#include <iostream>

// Declare globalCounter and set it to 0
int globalCounter = 0;

void increment() {
	globalCounter++;
}

// Program execution starts in main()
int main() {
	increment();
	increment();
	// Print to the console with cout
	std::cout << globalCounter << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

void counter() {
	static int count = 0;
	count++;
	// Print to the console with cout
	std::cout << count << std::endl;
}

// Program execution starts in main()
int main() {
	counter();
	counter();
	counter();
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a local variable outside the block where it was declared.
  2. Shadowing a global with a local of the same name and changing the wrong one.
  3. Expecting a normal local to keep its value between calls, when it is reset each call unless declared static.

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.