Chapter 26 Exercises: Inter-Language Communication

Exercise 26.1: Basic COBOL-C Interface

Difficulty: Beginner

Write a C function to_uppercase that accepts a character array and its length, and converts all lowercase letters to uppercase. Then write a COBOL program that:

  1. Defines a PIC X(50) string with mixed case
  2. Calls the C function with appropriate BY VALUE / BY REFERENCE conventions
  3. Displays the result

If using GnuCOBOL, compile both files and link them. If desk-checking, document each parameter's passing convention and explain why you chose it.

Exercise 26.2: Data Type Mapping

Difficulty: Beginner

Create a reference table that maps these specific COBOL data items to their C equivalents. For each pair, note any conversion issues:

  1. PIC S9(7)V99 COMP-3
  2. PIC X(25)
  3. PIC S9(9) COMP
  4. PIC S9(18) COMP
  5. PIC 9(8) DISPLAY (a date in YYYYMMDD format)

For items that have no direct C equivalent, describe how you would handle the conversion at the language boundary.

Exercise 26.3: String Conversion Utility

Difficulty: Intermediate

Write a reusable COBOL paragraph (or subprogram) called STRING-TO-C that:

  1. Accepts a COBOL PIC X(n) string and its declared length
  2. Determines the actual content length (strips trailing spaces)
  3. Produces a null-terminated C-compatible string
  4. Returns the actual content length

Also write the reverse: C-TO-STRING that accepts a null-terminated string and produces a space-padded COBOL string.

Test with edge cases: all-spaces input, maximum-length input, single-character input, and empty C string.

Exercise 26.4: Binary Interface Design

Difficulty: Intermediate

MedClaim needs to expose their claim validation logic to a C caller. The COBOL validation program currently uses these parameters:

01  LS-CLAIM-ID        PIC X(15).
01  LS-BILLED-AMOUNT   PIC S9(7)V99 COMP-3.
01  LS-SERVICE-DATE    PIC 9(8).
01  LS-DIAG-CODE       PIC X(7).
01  LS-RESULT-CODE     PIC X(2).
01  LS-RESULT-MSG      PIC X(100).

Design a "binary interface" version of this program. For each parameter: - Specify the new COBOL data type - Specify the corresponding C data type - Explain any conversion logic needed inside the COBOL program

Write the complete LINKAGE SECTION and the parameter conversion code in the PROCEDURE DIVISION.

Exercise 26.5: Error Handling Across Boundaries

Difficulty: Intermediate

Write a COBOL program that calls three C functions in sequence:

  1. validate_input — returns 0 for success, negative for various errors
  2. process_data — returns 0 for success, positive for warnings, negative for errors
  3. format_output — returns 0 for success, negative for errors

For each call, implement: - Pre-call parameter validation - Post-call return code checking with specific error messages - Trace logging showing parameters and return codes - A rollback strategy if any step fails (undo the effects of previous steps)

Exercise 26.6: JNI Bridge Design

Difficulty: Advanced

Design (on paper or in code) a complete three-layer integration for MedClaim's claim inquiry:

Java layer: ClaimService.java with methods getClaim(String claimId) and submitClaim(ClaimData data)

C glue layer: JNI native implementations that convert between Java types and COBOL binary types

COBOL layer: CLM-GET and CLM-SUBMIT programs with binary interfaces

Draw the data flow diagram. For getClaim, trace a claim ID from the Java String through JNI, through the C layer, into the COBOL program, and back. Identify every type conversion that occurs.

Exercise 26.7: Web Service Wrapper

Difficulty: Advanced

Write a COBOL program that calls a hypothetical C function http_post to submit a JSON payload to a REST API. The program should:

  1. Build a JSON string from COBOL data items (construct it character by character using STRING)
  2. Null-terminate the JSON string for C
  3. Call http_post with the URL, JSON payload, and a response buffer
  4. Parse a simple JSON response (extract a status code and message)
  5. Handle HTTP error codes (400, 401, 404, 500)

This exercise combines string manipulation, C interoperability, and practical web integration.

Exercise 26.8: Performance Comparison

Difficulty: Advanced (analytical)

Consider a string search operation that scans a 10,000-byte buffer for a 20-byte pattern. Write this operation in:

  1. Pure COBOL (using INSPECT TALLYING or a PERFORM loop)
  2. C (using strstr or a manual loop)
  3. A COBOL program that CALLs a C function for the search

For approach 3, analyze: does the overhead of the CALL (parameter setup, context switch, return) outweigh the performance gain of C's string handling? Under what conditions (buffer size, call frequency) does approach 3 become worthwhile?