COBOL Textbook

Part 4

Modern COBOL

GnuCOBOL, free format, calling C and Python, JSON, testing, and moving COBOL data in and out of everything else.

Part 4 is COBOL as it runs today under GnuCOBOL: next to other languages and formats rather than alone on a mainframe. Its five lessons cover source format, test data from Python, calls across the C boundary, JSON output and the compiler's dialect switch, and each one says where GnuCOBOL and IBM Enterprise COBOL part ways.

If you have only seen modern listings, start with free-format COBOL. It writes one payroll program twice, in the old fixed columns and in free format, and explains what columns 7, 8 and 12 were for. Generating fixed-width test data with Python builds three files that must agree to the byte: a Python script that writes 200 transaction records, a copybook, and a COBOL program that reads them back with control totals. Calling a C function from COBOL crosses the C ABI in both directions, with BY VALUE, BY REFERENCE and RETURNING one way and cob_init and cob_resolve the other. JSON GENERATE and JSON PARSE turns fixed-width records into JSON Lines and states plainly what version 3.2 lacks: JSON PARSE compiles with a warning and does nothing. The newest lesson, GnuCOBOL -std=ibm and other dialects, reads what each dialect file changes and shows one program printing different numbers under the default dialect and under -std=ibm.

Where a lesson quotes figures from a Python companion script, that script was run. Two of the COBOL listings here, free format and the fixed-width test data, were compiled and run with GnuCOBOL 3.2 on 29 October 2026; the other three are still labelled desk-checked. Planned for this part: embedded SQL (described against a named precompiler), unit testing COBOL, and calling COBOL from Python through a shared library.

  1. 4.1

    Generating fixed-width test data with Python and reading it in COBOL

    A Python script writes a 64-byte fixed-width transaction file, a copybook pins the layout, and a GnuCOBOL program reads it back and prints control totals.

    7 min

  2. 4.2

    Free-format COBOL in GnuCOBOL: what changed from columns 8 and 12

    The same COBOL program written twice, in fixed format and in free format, with the column rules that shaped it and where GnuCOBOL and IBM disagree.

    12 min

  3. 4.3

    Calling a C function from COBOL, and being called back

    CALL BY VALUE, BY REFERENCE and RETURNING across the C ABI, then the other direction with cob_init and cob_resolve, with both listings and the build lines.

    7 min

  4. 4.4

    COBOL JSON GENERATE and JSON PARSE: what GnuCOBOL 3.2 can do

    A GnuCOBOL program that turns fixed-width records into JSON Lines with JSON GENERATE, the exact output it should write, and why JSON PARSE does nothing in 3.2.

    15 min

  5. 4.5

    GnuCOBOL -std=ibm and other dialects: what each one changes

    What cobc -std=ibm actually switches in GnuCOBOL 3.2, read from its dialect files, and one program whose output changes when the dialect does.

    11 min