Bird
Raised Fist0
C++programming~5 mins

Scope of variables in C++ - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Scope of variables
O(n)
Understanding Time Complexity

We want to see how the time cost changes when we use variables in different places in code.

How does where a variable is declared affect how many times code runs?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


for (int i = 0; i < n; i++) {
    int x = i * 2;
    // do something with x
}

int y = 0;
for (int i = 0; i < n; i++) {
    y += i;
}
    

This code shows two loops: one declares a variable inside the loop, the other outside.

Identify Repeating Operations

Look at the loops and variable declarations that repeat.

  • Primary operation: The for loops run n times each.
  • How many times: Each loop runs exactly n times.
  • Variable declarations: The variable inside the first loop is created and destroyed each time.
How Execution Grows With Input

As n grows, the loops run more times, but variable scope does not add extra loops.

Input Size (n)Approx. Operations
10About 20 loop steps (2 loops x 10 each)
100About 200 loop steps
1000About 2000 loop steps

Pattern observation: The total steps grow linearly with n, regardless of where variables are declared.

Final Time Complexity

Time Complexity: O(n)

This means the time grows in a straight line as the input size n grows.

Common Mistake

[X] Wrong: "Declaring a variable inside a loop makes the code slower by adding extra loops."

[OK] Correct: Declaring a variable inside a loop does not add more loops; it only creates the variable each time, which is very fast and does not change the main time growth.

Interview Connect

Understanding how variable scope affects performance helps you write clear and efficient code, a skill valued in real projects and interviews.

Self-Check

"What if we moved the variable declared inside the loop to outside the loop? How would the time complexity change?"

Practice

(1/5)
1. What is the scope of a variable declared inside a function in C++?
easy
A. It can only be used inside that function.
B. It can be used anywhere in the program.
C. It can be used only in the main function.
D. It can be used in all functions after declaration.

Solution

  1. Step 1: Understand local variable scope

    A variable declared inside a function is local to that function only.
  2. Step 2: Check usage outside the function

    Such variables cannot be accessed outside their function block.
  3. Final Answer:

    It can only be used inside that function. -> Option A
  4. Quick Check:

    Local variable scope = inside function only [OK]
Hint: Local variables live only inside their function block [OK]
Common Mistakes:
  • Thinking local variables are accessible globally
  • Confusing local and global variable scopes
2. Which of the following is the correct way to declare a global variable in C++?
easy
A. int x; inside main() function
B. int x; outside all functions
C. int x; inside a block { }
D. int x; inside a for loop

Solution

  1. Step 1: Identify global variable declaration

    Global variables must be declared outside all functions and blocks.
  2. Step 2: Check each option

    Only int x; outside all functions declares the variable outside all functions, making it global.
  3. Final Answer:

    int x; outside all functions -> Option B
  4. Quick Check:

    Global variable declared outside functions [OK]
Hint: Global variables declared outside all functions [OK]
Common Mistakes:
  • Declaring global variables inside functions
  • Declaring variables inside loops thinking they are global
3. What will be the output of the following C++ code?
int x = 5;
void func() {
    int x = 10;
    std::cout << x << std::endl;
}
int main() {
    func();
    std::cout << x << std::endl;
    return 0;
}
medium
A. 10\n5
B. 10\n10
C. 5\n5
D. 5\n10

Solution

  1. Step 1: Analyze variable shadowing in func()

    Inside func(), local x = 10 shadows global x = 5, so prints 10.
  2. Step 2: Analyze main() output

    After func(), main() prints global x = 5.
  3. Final Answer:

    10\n5 -> Option A
  4. Quick Check:

    Local shadows global, prints 10 then 5 [OK]
Hint: Local variable shadows global inside function [OK]
Common Mistakes:
  • Assuming global x changes inside func()
  • Confusing which x is printed in main()
4. Identify the error in the following C++ code:
int main() {
    std::cout << x << std::endl;
    int x = 5;
    return 0;
}
medium
A. Error: x declared twice
B. No error, prints 5
C. Error: Missing semicolon
D. Error: x used before declaration

Solution

  1. Step 1: Check variable usage before declaration

    The code tries to print x before it is declared.
  2. Step 2: Understand C++ variable initialization rules

    Variables must be declared before use; using x before declaration causes error.
  3. Final Answer:

    Error: x used before declaration -> Option D
  4. Quick Check:

    Use after declaration required [OK]
Hint: Declare variables before using them [OK]
Common Mistakes:
  • Assuming declaration order does not matter
  • Ignoring compiler errors about undeclared variables
5. Consider the following code snippet:
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?
hard
A. 10\n5 because local x shadows global, but global x updated before local declaration
B. 10\n1 because global x not updated due to local shadowing
C. Compilation error due to variable redeclaration
D. 5\n10 because global x updated first, then local x printed

Solution

  1. Step 1: Analyze variable declaration order

    The line x = 5; appears before the local variable x is declared in the same function.
  2. 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.
  3. 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.
  4. Final Answer:

    Compilation error due to variable redeclaration -> Option C
  5. Quick Check:

    Using variable before declaration in same scope causes error [OK]
Hint: Cannot use variable before local declaration in same scope [OK]
Common Mistakes:
  • Thinking there's a compilation error on x=5
  • Believing local shadowing prevents global update before declaration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard