Scope of objects in C and C++

An object is visible in a block or source file if its data type and declared name are known within the block or source file. The region where an object is visible is referred to as its scope. In z/OS® Debugger, an object can be a variable or function and is also used to refer to line numbers.

Note: The use of an object here is not to be confused with a C++ object. Any reference to C++ will be qualified as such.
In ANSI C, the four kinds of scope are:
  • Block
  • File
  • Function
  • Function prototype

For C++, in addition to the scopes defined for C, it also has the class scope.

An object has block scope if its declaration is located inside a block. An object with block scope is visible from the point where it is declared to the closing brace (}) that terminates the block.

An object has file scope if its definition appears outside of any block. Such an object is visible from the point where it is declared to the end of the source file. In z/OS Debugger, if you are qualified to the compilation unit with the file static variables, file static and global variables are always visible.

The only type of object with function scope is a label name.

An object has function prototype scope if its declaration appears within the list of parameters in a function prototype.

A class member has class scope if its declaration is located inside a class.

You cannot reference objects that are visible at function prototype scope, but you can reference ones that are visible at file or block scope if:
  • For C variables and functions, the source file was compiled with TEST(SYM) and the object was referenced somewhere within the source.
  • For C variables declared in a block that is nested in another block, the source file was compiled with TEST(SYM, BLOCK).
  • For line numbers, the source file was compiled with TEST(LINE) GONUMBER.
  • For labels, the source file was compiled with TEST(SYM, PATH). In some cases (for example, when using GOTO), labels can be referenced if the source file was compiled with TEST(SYM, NOPATH).

z/OS Debugger follows the same scoping rules as ANSI, except that it handles objects at file scope differently. An object at file scope can be referenced from within z/OS Debugger at any point in the source file, not just from the point in the source file where it is declared. z/OS Debugger session variables always have a higher scope than program variables, and consequently have higher precedence than a program variable with the same name. The program variable can always be accessed through qualification.

In addition, z/OS Debugger supports the referencing of variables in multiple load modules. Multiple load modules are managed through the C library functions dllload(), dllfree(), fetch(), and release().

Example: referencing variables and setting breakpoints in C and C++ blocks