1

I am trying to call a C function from COBOL and expecting a reply from it. I am new to this interfacing.

COBOL code:

ENTER C "ADD" USING A,B.

C code:

int ADD(int a,int b)
{
    return a+b;
}

I want to get the sum value from the C function for further processing in COBOL.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
GoudSaab
  • 13
  • 2
  • ... and the question is? Note: to `CALL` that you'd pass the data `BY VALUE` from the COBOL side. If you want `BY REFERENCE` you'd have to use pointers - `int *a` - and then would also see changes in those on the COBOL side. – Simon Sobisch Sep 25 '21 at 20:12
  • I am getting an error while calling by reference, error 44 and 45 while compiling COBOL It would be great if you have some example or link which is getting return value to cobol from c – GoudSaab Sep 26 '21 at 10:57
  • any suggestions @SimonSobisch – GoudSaab Sep 26 '21 at 16:40
  • You must get an error during call as the C part is using `int` not `int *`. If you change this and call `BY REFERENCE` with variables defined as "Numeric data item described as COMP-5 with PICTURE S9(9) or NATIVE-4" (according to the COBOL manual from HP) then the C could adjust the variables and COBOL would see it (you may or may not want that). Other than that you could try to have COBOL do `CALL "ADD" USING BY VALUE` and afterwards check the `RETURN-CODE` register. If both don't work I'd have a look at the `ENTER` statement (but that's a complete HP extension). – Simon Sobisch Sep 26 '21 at 16:44
  • I do not know Tandem, but when I call a C program, I have to use `CALL` and `USING`. – Christoph S. Sep 26 '21 at 21:24

3 Answers3

0

In COBOL

EXTENDED-STORAGE SECTION.
01 MYVAR EXTERNAL.
   05 DATA-01 PIC X(20).

In C

 /*Add necessary includes */
 extern char MYVAR[21];

 void change_Cobol_Variable()
 {
   /*you can use MYVAR as normal C-variable*/
   sprintf(MYVAR, "%s","Something");
 }

If it is integer declare appropriate variables as per your need :)

Loki
  • 1,180
  • 9
  • 13
  • Does HP always add one (low-value) byte to the COBOL definitions, only does this for `EXTERNAL` variables or is there an error in this example that could lead to memory corruption when the text `sprintf`'d is 20 characters long and the final low-value will be written to the position 21? How does this answer relate to the original question of "how to get the C sum for processing in COBOL" (as this answer is about an alphanumeric group of size 20, not an integer)? – Simon Sobisch Oct 18 '21 at 06:08
  • The above answer is an example! It's just psuedo code! – Loki Oct 19 '21 at 12:55
  • Can you please edit the answer - ideally with _real_ code and integers, if not possible then with an explicit note about which parts are pseudo code and need an adjustment? – Simon Sobisch Oct 20 '21 at 09:20
0

In Cobol:

EXTENDED-STORAGE SECTION.
01 C EXTERNAL.
   05 DATA-01 PIC 9(1).

In C:

/*Add necessary includes */
extern int C;

void ADD(int A,int B)
{
/*you can use C as normal C-variable*/
  C=A+B;
}
Loki
  • 1,180
  • 9
  • 13
0

You can use GIVING

ENTER C "ADD" USING A,B GIVING ANS.

so your a+b value will be stored in ANS