Block Scope

Identifiers declared inside a block have block scope, which begins at the identifier’s declaration and ends at the terminating right brace (}) of the block in which the identifier is declared. Local variables have block scope, as do function parameters. Any block can contain variable declarations. When blocks are nested and an identifier in an outer block has the same name as an identifier in an inner block, the identifier in the outer block is “hidden” until the inner block terminates. The inner block “sees” its own local identifier’s value and not that of the enclosing block’s identically named identifier. Local variables declared static still have block scope, even though they exist from the time the program begins execution. Storage duration does not affect an identifier’s scope.


Image Common Programming Error 6.7

Accidentally using the same name for an identifier in an inner block that’s used for an identifier in an outer block, when in fact you want the identifier in the outer block to be active for the duration of the inner block, is typically a logic error.



Image Error-Prevention Tip 6.6

Avoid variable names that hide names in outer scopes.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.97