Define host variables based on C #defines and typedefs
When the C preprocessor runs on a source file, it expands
the contents of all C header files where they are included within
the source file. This expansion makes it possible to use host variables
in IBM®
Informix® ESQL/C source
files based on types and #define and typedef statements
in C header files. The examples given here indicate some of the advantages
of sharing header files. In the following example, the same C header
file is used by both Informix
ESQL/C and
C source files.
Figure 1. ESQL/C and
C excerpt that share a C header file
/*header file i.h*/
#define LEN 15
typedef struct customer_t{
int c_no;
char fname[LEN];
char lname[LEN];
} CUST_REC;
⋮
/*cust.ec*/
#include <stdio.h>
EXEC SQL BEGIN DECLARE SECTION;
#include "i.h"
EXEC SQL END DECLARE SECTION;
int main()
{
EXEC SQL BEGIN DECLARE SECTION;
CUST_REC cust;
EXEC SQL END DECLARE SECTION;
⋮
}
/*name.c*/
#include “i.h”
int main ()
{...
CUST_REC cust;
⋮
}
In the following example, the Informix
ESQL/C source
file contains a host variable based on a type defined in the time.h system-header
file.
Figure 2. ESQL/C
excerpt that uses a host variable of the type defined in a C system-header
file
/*time.ec*/
#include <time.h>
main ()
{...
EXEC SQL BEGIN DECLARE SECTION;
time_t time_hostvar;
EXEC SQL END DECLARE SECTION;
⋮
}
A C header file can be included anywhere inside the Informix ESQL/C source file. However, to define host variables in Informix ESQL/C files based on #defines and typedefs defined in C header files, you must include the C header file within the EXEC SQL declare section.
Contrast the example in Figure 3, which leads to error
-33051: Syntax error on identifier or symbol ‘name_hostvar with
the example in Figure 1 which
does not. The only difference is that in the example in Figure 1, the C header file with
the #define and the typedef that is used in the EXEC
SQL declare section is included within that declare section.
Figure 3. ESQL/C excerpt
that defines a host variable based on a c header file included outside
the declare section
/*header file i.h*/
#define LEN 15
typedef struct customer_t{
int c_no;
char fname[LEN];
char lname[LEN];
} CUST_REC;
⋮
/*cust.ec*/
#include "i.h"
int main()
{
EXEC SQL BEGIN DECLARE SECTION;
CUST_REC cust;
⋮
}
⋮
...Leads to error -33051...