Calling a C function from Cobol

So, you may not like C, but imagine a world where it’s possible to call C from Cobol!

That world is this world! Here is an example of a C function say() which prints out two 6-character strings:

#include <stdio.h>

int say(char hello[6], char world[6])
{
    int i;
    for (i=0; i<6; i=i+1)
        putchar(hello[i]);
    for (i=0; i<6; i=i+1)
        putchar(world[i]);
    putchar('\n');
    return 0;
}

This code is then compiled into an object file:

gcc -c say.c -o say.o

Here is the corresponding Cobol program which calls it.

identification division.
program-id. hello.
environment division.
data division.
working-storage section.
01 hello pic x(6) value "hello ".
01 world pic x(6) value "world!".
procedure division.
  call "say" using hello world.
  stop run.

Which is compiled in the following manner:

cobc -x -Wall -free -O hello.cob say.o

Okay, so this *looks* easy… and it is. But it gets harder with integers, and arrays of integers – largely because of the differences between numeric values in Cobol and C. Note that because cobc compiles to C, this shouldn’t be an issue, but it seems to be.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s