Previous Topic: Coding Embedded SQL in CNext Topic: Using Preprocessor Options


Rules for Coding Host-Variables in C

When you code host-variable declarations in C, you must comply with the following requirements:

C Data Types Compared to SQL Data Types

C Data Type

Example

SQL Data Type

single char

char x;

CHAR

char array

char x[10]

VARCHAR

char structure containing exactly one short variable followed by exactly one char array.

char struct {
short len1;
char text[9];
} hv1;

VARCHAR

decimal(precision, scale)

decimal(5,0)

DECIMAL

short or short int

short hv1;

SMALLINT

int or long int

int hv1;

INTEGER

float

float hv1;

FLOAT

double

double hv1;

FLOAT

char array

char dateHv[11];

DATE

char array

char timeHv[9];

TIME

char array

char timeStampHv[23];

TIMESTAMP

Comments on C Data Types

char array

The C char array is treated as a null-terminated string.

char hv1[10] is equivalent to VARCHAR(9).

The VARCHAR length is set based on the number of bytes preceding the null-terminating byte.

Char arrays for DATE, TIME, and TIMESTAMP also need the null-terminating byte.

char structure

The char array for the text is not null terminated.

decimal

Use of decimal requires #include decimal.h (provided by the IBM C Compiler), and the compiler option must not be ANSI. Precision and scale is optional. To print decimal values, use (with printf):

 %D(precision,scale)
host structures

In DATACOM mode, a host variable structure may be used. The host structure must be a C structure containing only valid host variables. The host structure must not be nested within another structure.

indicator variables

In DATACOM mode, a host indicator structure or array may be used. The structure or array must only contain short C variables. The indicator structure must not be nested within another structure.

INCLUDEs

INCLUDEs in the C language are basically the same as in PL/I except note the following: