Simple block scope

suggest change

The scope of a variable in a block { ... } begins after its declaration and ends at the end of the block. If there is a nested block, the inner block can hide a variable declared in the outer block.

{
    int x = 100;
    //   ^
    //   Scope of `x` begins here
    //
}   // <- Scope of `x` ends here

If a nested block starts within an outer block, a variable declared in the inner block with the same name hides the one in the outer block.

{
    int x = 100;

    {
        int x = 200;

        std::cout << x;  // <- Output is 200
    }

    std::cout << x;  // <- Output is 100
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents