RAD Studio |  |

Scope
See Also
Collapse All

The scope of an identifier is that part of the program in which the identifier can be used to access its object. There are six categories of scope: block (or local), function, function prototype, file, class (C++ only), condition (C++ only), and namespace (C++ only). These depend on how and where identifiers are declared.

- Block. The scope of an identifier with block (or local) scope starts at the declaration point and ends at the end of the block containing the declaration (such a block is known as the enclosing block). Parameter declarations with a function definition also have block scope, limited to the scope of the block that defines the function.
- Function. The only identifiers having function scope are statement labels. Label names can be used with goto statements anywhere in the function in which the label is declared. Labels are declared implicitly by writing label_name: followed by a statement. Label names must be unique within a function.
- Function prototype. Identifiers declared within the list of parameter declarations in a function prototype (not part of a function definition) have function prototype scope. This scope ends at the end of the function prototype.
- File. File scope identifiers, also known as globals, are declared outside of all blocks and classes; their scope is from the point of declaration to the end of the source file.
- Class (C++). A class is a named collection of members, including data structures and functions that act on them. Class scope applies to the names of the members of a particular class. Classes and their objects have many special access and scoping rules; see Classes.
- Condition (C++). Declarations in conditions are supported. Variables can be declared within the expression of if, while, and switch statements. The scope of the variable is that of the statement. In the case of an if statement, the variable is also in scope for the else block.
- namespace (C++). A namespace is a logical grouping of program entities (e.g. identifiers, classes, and functions). Namespaces are open, that is, they can span multiple compilation units. You can think of a namespace as introducing a named scope, similar in many ways to a class in C++. See the help for the namespace keyword for more inforrmation on how to declare and use namespaces.

Name spaces

Name space is the scope within which an identifier must be unique. Note that a C++ namespace extends this concept by allowing you to give the scope a name. In addition to the named-scoping capability of C++, the C programming language uses four distinct classes of identifiers:

- goto label names. These must be unique within the function in which they are declared.
- Structure, union, and enumeration tags. These must be unique within the block in which they are defined. Tags declared outside of any function must be unique.
- Structure and union member names. These must be unique within the structure or union in which they are defined. There is no restriction on the type or offset of members with the same member name in different structures.
- Variables, typedefs, functions, and enumeration members. These must be unique within the scope in which they are defined. Externally declared identifiers must be unique among externally declared variables.

See Also

Declarations

Objects

Storage Classes And Types

Visibility

Duration

Linkage

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!