Scope of host variables

The scope of reference, or the scope, of a host variable is that portion of the program in which the host variable can be accessed.

The placement of the Informix® ESQL/C declaration statement determines the scope of the variable as follows:
  • If the declaration statement is inside a program block, the variable is local to that program block.

    Only statements within that program block can access the variable.

  • If the declaration statement is outside a program block, the variable is modular.

    All program blocks that occur after the declaration can access the variable.

Host variables that you declare within a block of code are local to that block. You define a block of code with a pair of curly braces, { }.

For example, the host variable blk_int in the following figure is valid only in the block of code between the curly braces, whereas p_int is valid both inside and outside the block.
Figure 1: Declaring host variables inside and outside a code block
EXEC SQL BEGIN DECLARE SECTION;
     int p_int;
EXEC SQL END DECLARE SECTION;
⋮
EXEC SQL select customer_num into :p_int from customer
   where lname = "Miller";
⋮
{
   EXEC SQL BEGIN DECLARE SECTION;
      int blk_int;
   EXEC SQL END DECLARE SECTION;

   blk_int = p_int;
   
⋮
   EXEC SQL select customer_num into :blk_int from customer
      where lname = "Miller";
⋮
}

You can nest blocks up to 16 levels. The global level counts as level one.

The following C rules govern the scope of Informix ESQL/C host variables as well:
  • A host variable is an automatic variable unless you explicitly define it as an external or static variable or unless it is defined outside of any function.
  • A host variable that a function declares is local to that function and masks a definition with the same name outside the function.
  • You cannot define a host variable more than once in the same block of code.