Compaq COBOL AAQ2G1FTK User Manual

Page 459

Advertising
background image

Interprogram Communication

12.7 Calling Non-COBOL Programs from Compaq COBOL

12.7.3 Calling a C Program

Calling a program or routine that is written in C allows you to take advantage of
features of that language. Example 12–16 features a C routine that can be called
from a COBOL program.

Example 12–16 has two global external variables, _ _Argc and **_ _Argv. Note
that **_ _Argv[ ] has an extra level of indirection; it is a pointer to a pointer to an
array.

Example 12–16 C Routine to Be Called from a COBOL Program

/* crtn - c function to test use of argc and argv in c routines

*

called from Compaq COBOL

*/

#include "cobfunc.h"
#include <stdio.h>

extern int _ _Argc;
extern char **_ _Argv[];
#define argc _ _Argc
#define argv (*_ _Argv)

void crtn()
{

int i;

i = 0;
for (i = 0; i < argc; i++) {

printf("argv[%d] = %s\n", i, argv[i]);

}

}

Example 12–17 is a representation of how you can call a C program from your
Compaq COBOL application. In this case, the C routine crtn (in Example 12–16)
is called.

Example 12–17 Calling a C Program from a COBOL Program

IDENTIFICATION DIVISION.
PROGRAM-ID. CTEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
.
.
.

PROCEDURE DIVISION.
MAIN SECTION.
A001-MAIN.
.
.
.

CALL "crtn".
EXIT PROGRAM.

END PROGRAM CTEST.

For information on handling LIB$INITIALIZE when calling a C program, see
Appendix B.

Interprogram Communication 12–29

Advertising