Scope of variables tells us where a variable can be used in the code. It helps keep things organized and avoids confusion.
Scope of variables in C++
/* Variable declared inside a function - local scope */ void example() { int x = 5; // x is local to example() } /* Variable declared outside functions - global scope */ int y = 10; // y is global int main() { // y can be used here return 0; }
Variables declared inside functions or blocks have local scope.
Variables declared outside all functions have global scope and can be used anywhere after declaration.
void func() { int a = 3; // 'a' is local to func // 'a' cannot be used outside func }
int b = 7; // global variable int main() { // 'b' can be used here return 0; }
int x = 1; // global void f() { int x = 2; // local to f, hides global x // inside f, 'x' is 2 } int main() { // here 'x' is 1 return 0; }
This program shows a global variable and a local variable inside a function. The local variable is only visible inside the function, but the global variable is visible everywhere.
#include <iostream> int globalVar = 100; // global variable void show() { int localVar = 50; // local variable std::cout << "Inside show(): localVar = " << localVar << "\n"; std::cout << "Inside show(): globalVar = " << globalVar << "\n"; } int main() { show(); // std::cout << localVar; // This would cause an error because localVar is not visible here std::cout << "Inside main(): globalVar = " << globalVar << "\n"; return 0; }
Local variables are created when the function/block starts and destroyed when it ends.
Global variables exist for the whole program run but use them carefully to avoid confusion.
Variables with the same name in different scopes do not interfere with each other.
Scope defines where a variable can be used in the program.
Local variables live inside functions or blocks only.
Global variables can be used anywhere after they are declared.
Practice
Solution
Step 1: Understand local variable scope
A variable declared inside a function is local to that function only.Step 2: Check usage outside the function
Such variables cannot be accessed outside their function block.Final Answer:
It can only be used inside that function. -> Option AQuick Check:
Local variable scope = inside function only [OK]
- Thinking local variables are accessible globally
- Confusing local and global variable scopes
Solution
Step 1: Identify global variable declaration
Global variables must be declared outside all functions and blocks.Step 2: Check each option
Only int x; outside all functions declares the variable outside all functions, making it global.Final Answer:
int x; outside all functions -> Option BQuick Check:
Global variable declared outside functions [OK]
- Declaring global variables inside functions
- Declaring variables inside loops thinking they are global
int x = 5;
void func() {
int x = 10;
std::cout << x << std::endl;
}
int main() {
func();
std::cout << x << std::endl;
return 0;
}Solution
Step 1: Analyze variable shadowing in func()
Inside func(), local x = 10 shadows global x = 5, so prints 10.Step 2: Analyze main() output
After func(), main() prints global x = 5.Final Answer:
10\n5 -> Option AQuick Check:
Local shadows global, prints 10 then 5 [OK]
- Assuming global x changes inside func()
- Confusing which x is printed in main()
int main() {
std::cout << x << std::endl;
int x = 5;
return 0;
}Solution
Step 1: Check variable usage before declaration
The code tries to print x before it is declared.Step 2: Understand C++ variable initialization rules
Variables must be declared before use; using x before declaration causes error.Final Answer:
Error: x used before declaration -> Option DQuick Check:
Use after declaration required [OK]
- Assuming declaration order does not matter
- Ignoring compiler errors about undeclared variables
int x = 1;
void update() {
x = 5;
int x = 10;
std::cout << x << std::endl;
}
int main() {
update();
std::cout << x << std::endl;
return 0;
}What will be the output and why?
Solution
Step 1: Analyze variable declaration order
The line x = 5; appears before the local variable x is declared in the same function.Step 2: Understand C++ rules on variable usage
Using a variable before its local declaration in the same scope is illegal and causes a compilation error.Step 3: Reason about the error
Because local x is declared after x = 5;, the assignment tries to assign to local x before its declaration, which is invalid.Final Answer:
Compilation error due to variable redeclaration -> Option CQuick Check:
Using variable before declaration in same scope causes error [OK]
- Thinking there's a compilation error on x=5
- Believing local shadowing prevents global update before declaration
Ready to prove it?
Pass the test and this pattern moves unknown
