scope code block : block scope variable « Language Basics « C++ Tutorial






#include <iostream>

using namespace std;

void func();

int main()
{
    int var = 5;
    cout << "In main() var is: " << var << "\n\n";

    func();

    cout << "Back in main() var is: " << var << "\n\n";
    {
        cout << "In main() in a new scope var is: " << var << "\n\n";
        cout << "Creating new var in new scope.\n";
        int var = 10;
        cout << "In main() in a new scope var is: " << var << "\n\n";
    }

    cout << "At end of main() var is: " << var << "\n";

    return 0;
}

void func()
{
    int var = -5;  // local variable in func()
    cout << "In func() var is: " << var << "\n\n";
}








1.6.block scope variable
1.6.1.Variable block scope
1.6.2.Variables can be local to a block
1.6.3.Inner block variable scope
1.6.4.Names in inner scopes can hide names in outer scopes.
1.6.5.Using the scope resolution operator: '::'
1.6.6.global and block scope
1.6.7.scope code block
1.6.8.global variables across functions
1.6.9.Using the scope resolution operator (2)