INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 1: Programming with INFORMIX-ESQL/C
Home Contents Index Master Index New Book

Using ESQL/C Preprocessor Directives

You can use the following capabilities of the ESQL/C preprocessor when you design your embedded code:

As with embedded SQL statements, you can use either of two formats for ESQL/C preprocessor directives:

In either of these formats, replace preprocessor_directive with one of the valid processor directives that the following sections describe. You must terminate these directives with a semicolon (;).

Important: The EXEC SQL keywords conform to ANSI standards.
The ESQL/C preprocessor works in two stages. In Stage 1, it acts as a preprocessor for the ESQL/C code. In Stage 2, it converts all of the embedded SQL code to C code. The ESQL/C preprocessor handles the preprocessor directives at different stages, as follows:

The remainder of this section describes each of the ESQL/C preprocessor directives in more detail.

The include Directive

The include directive allows you to specify a file to include within your ESQL/C program. When it reaches an include directive, the ESQL/C preprocessor places the contents of the specified file into the ESQL/C source file. Stage 1 of the ESQL/C preprocessor reads the contents of filename into the current file at the position of the include directive.

You can use the include preprocessor directive in either of the following two formats:

In either of these formats, replace filename with the name of the file to be included in your program. You can specify filename with or without quotation marks around the filename. However, if you use a full pathname, you must enclose the pathname in quotation marks. The following example shows how to use full pathnames on a Windows system.

If you omit the quotes around the filename, ESQL/C changes the filename to lowercase characters. If you omit the pathname, the ESQL/C preprocessor checks the preprocessor search path for the file. For more information on this search path, see "Naming the Location of Include Files".

You can use include for the following types of files:

    You do not have to use the .h file extension for an ESQL/C header file; the compiler assumes that your program refers to a file with a .h extension. The following examples show valid statements to include ESQL/C header files:

EXEC SQL include varchar.h;
$include sqlda;
EXEC SQL include sqlstype;

    You must specify the exact name of the file that you wish to include. The compiler does not assume the .h extension when you include a header file that is not an ESQL/C header file.

EXEC SQL include constant_defs;
EXEC SQL include "/work/esqlcstuff/constant_defs";
$include typedefs.h;
EXEC SQL include "/work/include/typedefs.h";

You must use the include directive if the file you specify contains SQL statements.

Tip: To include system header files, use the standard #include directive of the C preprocessor. The #include of C includes a file after ESQL/C preprocessing.

The define and undef Directives

The ESQL/C preprocessor allows you to create simple variables that are available only to the ESQL/C preprocessor. Informix calls these variables definitions. The ESQL/C preprocessor manages these definitions with two directives:

define

creates a name-flag definition. The scope of this definition is from the point where you define it to the end of the ESQL/C source file.

undef

removes a name flag that EXEC SQL define or $define creates.

The ESQL/C preprocessor rather than the C preprocessor (which processes #define and #undef) processes these directives. The ESQL/C preprocessor creates (define) or removes (undef) these definitions in stage 1 of preprocessing.

The ESQL/C define directive can create definitions with the following formats:

    The following examples show the two ways to define a Boolean symbol that is called TRANS:

EXEC SQL define TRANS;
$define TRANS;

    The following examples show both formats for two integer constants, MAXROWS (with a value of 25), and USETRANSACTIONS (with a value of 1):

EXEC SQL define MAXROWS 25;
$define MAXROWS 25;

EXEC SQL define USETRANSACTIONS 1;
$define USETRANSACTIONS 1;

Unlike the C #define statement, the define directive does not support string constants or macros of statements that receive values at runtime.

You can override definitions from the esql command line with the -ED and -EU options. For more information about these options, see "Defining and Undefining Definitions While Preprocessing".

The ifdef, ifndef, elif, else, and endif Directives

The ESQL/C preprocessor supports the following directives for conditional compilation:

ifdef

tests a name and executes subsequent statements if define has created the name.

ifndef

tests a name and executes subsequent statements if define has not created the name.

elif

begins an alternative section to an ifdef or ifndef condition and checks for the presence of another define.

else

begins an alternative section to an ifdef or ifndef condition.

endif

closes an ifdef or ifndef condition.

In the following example, the BEGIN WORK statement only compiles if you have previously defined the name USETRANSACTIONS with a define directive:

The following example uses the dollar sign ($) format to perform the same task:

The ESQL/C preprocessor does not support a general if directive; it supports only the ifdef and ifndef statements that test whether a name has been defined with define.

The ESQL/C preprocessor processes these directives rather than the C preprocessor (which processes #ifdef and #ifndef). The ESQL/C preprocessor processes conditional compilation definitions in Stage 2 of the preprocessing.




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.