Mixing C and BCX code
BCX allows you pass inline "C" code using the "!" operator.
! inline "C" code operator
Syntax:
! /** C statement is placed here */
|
Purpose: Mix a block of C code with BCX code.
Example: The following sample shows how to create a C coded local DWORD variable.
PRINT GetMachineName$() FUNCTION GetMachineName$ LOCAL A$ ! DWORD b; /** allocate a local variable in "C" and include C comments! */ b = 256 GetComputerName(A$,&b) ' the & operator means "PASS THE ADDRESS" of b FUNCTION = A$ END FUNCTION
$CCODE directive
Purpose: BCX allows you pass inline "C" code using the $CCODE directive. A $CCODE directive is placed before and after the "C" code.
Syntax: $CCODE /** C statements go here */ $CCODE |
Example:
PRINT "BASIC Code Here." $CCODE // declare the variables: int nNumber; int *pPointer; // now, give a value to them: nNumber = 15; pPointer = &nNumber; // print out the value of nNumber: printf("nNumber is equal to : %d\n", nNumber); // now, alter nNumber through pPointer: *pPointer = 25; // prove that nNumber has changed as a result of the above // by printing its value again: printf("nNumber is equal to : %d\n", nNumber); $CCODE PRINT "BASIC Code Here."
Result:
BASIC Code Here. nNumber is equal to : 15 nNumber is equal to : 25 BASIC Code Here.
Remarks: When embedding a complete function in C into BCX one way to deal with the function prototype is to put it into a C header file(*.h), and #INCLUDE the C header at the beginning of the program. Be sure to use only C code in the header. A second way to deal with the function prototype is to use the $HEADER directive.
$HEADER directive
Purpose: The $HEADER directive works like $CCODE except everything sandwiched between two $HEADER statements is placed at the module level of the emitted "C" source. $HEADER is useful particularly for pragma statements and declaring prototypes for inlined C source functions. A $HEADER directive is placed before and after the "C" code.
Syntax: $HEADER /** C statements go here */ $HEADER |
Example:
$HEADER #define KitchenSinkIsIncluded $HEADER $HEADER #ifndef KitchenSinkIsIncluded #include <KitchenSink.h> #else #define CallThePlumber 1 #endif $HEADER
$CPROTO directive
Purpose: The $CPROTO directive is used for declaring prototypes for inlined C source functions.
Syntax 1:
|
Remarks: When using $CPROTO with a C language prototype, a space-exclamation mark-space must precede the prototype. Also, in C, a semicolon is required at the end of of the prototype.
Example:
$CPROTO
!char
*
Remove_All_White_Space(
char
*
)
;DIM
a$DIM
b$ a$=
" this is a test "
b$=
Remove_All_White_Space(
a$)
END
PROGRAM
!char
*
Remove_All_White_Space(
char
*
str1)
! { !char
*
obuf,*
nbuf; !if
(
str1)
! { !for
(
obuf=
str1,nbuf=
str1;*
obuf;+
+
obuf)
! { !if
(
!isspace(
*
obuf)
)
!*
nbuf+
+
=
*
obuf; ! } !*
nbuf=
0; ! } !return
str1; ! }
Syntax 2:
|
Remarks: $CPROTO also will accept a BASIC FUNCTION or SUB declaration and convert it to the approriate C language code.
Example:
$CPROTO
FUNCTION
Remove_All_White_Space$(
a$)
DIM
a$DIM
b$ a$=
" this is a test "
b$=
Remove_All_White_Space(
a$)
END
PROGRAM
!char
*
Remove_All_White_Space(
char
*
str1)
! { !char
*
obuf,*
nbuf; !if
(
str1)
! { !for
(
obuf=
str1,nbuf=
str1;*
obuf;+
+
obuf)
! { !if
(
!isspace(
*
obuf)
)
!*
nbuf+
+
=
*
obuf; ! } !*
nbuf=
0; ! } !return
str1; ! }