2. Embedded QUEL for C : C Variables and Data Types : The Scope of Variables
 
Share this page                  
The Scope of Variables
While the EQUEL precompiler understands the scope of a variable, in programs where this is important, you must ensure that the preprocessor's scoping of the variable coincides with that of the C compiler.
In programs without conflict between multiple variables of the same name declared with different scope, this issue can be ignored. The precompiler does not need to be made aware of scoping information, and it will consider all variables visible to it to belong to one global scope covering the entire source file. Under these circumstances, a second declaration of a particular variable name will generate an error message from the precompiler, and the second declaration will be ignored.
In programs where variable names conflict, or for any other reason scoping becomes an issue, you must observe the following rules to maintain a consistent understanding of scope between the EQUEL precompiler and the C compiler:
To declare a scope for a particular procedure, or randomly in your source code, use the ## signal with the opening and closing braces. The preprocessor considers all variables declared in these braces as local to that EQUEL scope. For example:
       if (error)
##     {
##         int i;  /* i is local */
           EQUEL statement using 'i'
##   }
This is true not only for C blocks, but also for EQUEL statements that are block structured, such as retrieve. The braces that delimit EQUEL blocks can also be used as local C blocks and can include variable declarations.
The above rule holds for fully enclosed declarations, such as in the example above or for variables local to a procedure. You can also declare arguments to procedures, but EQUEL may consider these global, depending on where you put the ## signal. For example:
        proc1( a )
##      int a;
##      {
            EQUEL statements using 'a'
##      }
In this context, variable "a" is global to the file, which, although legal, may conflict with a later procedure declaration:
    proc2( a )
##  char  *a;/* EQUEL complains about redeclaration*/
##  {
          EQUEL statements using 'a'
##  }
To solve this problem, put a ## signal on the procedure header and the parameter list. However, it is not necessary to make all of the parameters known to EQUEL, nor is it necessary to make the function return type known. The above problem of proc1 and proc2 having conflicting declarations of "a" could be solved as in the following example:
##   proc1( a )
##   int a;
##   {
         EQUEL statements using 'a'
##   }

##   proc2( a )
##   char  *a;      /* EQUEL does not give error */
##   {
         EQUEL statements using 'a'
##   }
Note that this does not imply that EQUEL supports function declarations. EQUEL only makes use of the scope information.
The rules for the scope of a ##define value are the same as for a variable. If the ##define statement is in the outermost scope of the file, it is processed like a C #define and remains in effect for the whole file. If the ##define is in a particular EQUEL scope (that is, in a procedure with a ## on the opening and closing braces), then that EQUEL scope is the scope of the defined name.
The following program fragments demonstrate a complete EQUEL/C program syntax:
##   /* Global declarations */
##   int      globvar;

     main()
##   {
##        int arg;

          MAIN program uses 'arg' and 'globvar'
##  }

##   proc( arg )
##   int   arg;
##  {
##      float sal;

         C and EQUEL code using 'arg', 'sal' 
         and 'globvar'
##  }