Guidelines for Writing C Procedures
Use the following syntax when writing a C procedure:
procname([parameters])
{
processing statements
}
Note: You cannot name your C procedure “main,” and the procedure must not be static. You can call any C procedure from 4GL except the “main” function.
Follow these guidelines for passing parameters to C procedures:
• Pass an integer as four bytes by value (or by reference if byref is specified).
• Pass a smallint as four bytes by value (or by reference if byref is specified).
• Pass a float as a double-format float by value (or by reference, if byref is specified).
To ensure full portability, pass all floating point parameters to C procedures using the byref qualifier. For example, the following code fragment declares some variables and calls a C procedure, passing the procedure a floating-point parameter:
/* variable declarations */
test_float = float;
test_integer = integer;
test_return = integer
...
test_return = callproc myCproc (test_integer,
byref(test_float));
The corresponding C procedure is declared as follows:
Long
myCproc (ivalue, fvalue)
long ivalue;
double *fvalue;
{
processing statements
}
• Pass a string as a pointer to a null-terminated string as follows:
– Pass fixed-length string types (c, char) with trailing blanks up to their full length.
– Pass variable-length string types (text, varchar) without trailing blanks.
• In this call to a procedure “q”:
callproc q (1 + 2, 2.3, 'This is a string');
the following declarations are required:
q(x, y, z)
long x;
double y; char *z;
{...
To receive x and y that are passed by reference, make the following changes to their formal argument declarations:
long *x;
double *y;
No changes are necessary to receive z that is passed by reference.
A C procedure must return an int, a long, a double, or a char * value, as shown in the following examples.
• To return an integer:
int
reti()
{
return 10;
}
• To return a floating-point value:
double
retf()
{
return 10.5;
}
• To return a character string:
char *
rets()
{
return "Returned from rets";
}
Any C procedure that returns a char * value to 4GL must return a pointer to a valid address (a string constant or static or global character buffer). The procedure cannot return a pointer to a local character buffer.