Breaking
May 1, 2025

A Gentle Introduction to COBOL Maya Posch | usagoldmines.com

As the Common Business Oriented Language, COBOL has a long and storied history. To this day it’s quite literally the financial bedrock for banks, businesses and financial institutions, running largely unnoticed by the world on mainframes and similar high-reliability computer systems. That said, as a domain-specific language targeting boring business things it doesn’t quite get the attention or hype as general purpose programming or scripting languages. Its main characteristic in the public eye appears be that it’s ‘boring’.

Despite this, COBOL is a very effective language for writing data transactions, report generating and related tasks. Due to its narrow focus on business applications, it gets one started with very little fuss, is highly self-documenting, while providing native support for decimal calculations, and a range of I/O access and database types, even with mere files. Since version 2002 COBOL underwent a number of modernizations, such as free-form code, object-oriented programming and more.

Without further ado, let’s fetch an open-source COBOL toolchain and run it through its paces with a light COBOL tutorial.

Spoiled For Choice

It used to be that if you wanted to tinker with COBOL, you pretty much had to either have a mainframe system with OS/360 or similar kicking around, or, starting in 1999, hurl yourself at setting up a mainframe system using the Hercules mainframe emulator. Things got a lot more hobbyist & student friendly in 2002 with the release of GnuCOBOL, formerly OpenCOBOL, which translates COBOL into C code before compiling it into a binary.

While serviceable, GnuCOBOL is not a compiler, and does not claim any level of standard adherence despite scoring quite high against the NIST test suite. Fortunately, The GNU Compiler Collection (GCC) just got updated with a brand-new COBOL frontend (gcobol) in the 15.1 release. The only negative is that for now it is Linux-only, but if your distribution of choice already has it in the repository, you can fetch it there easily. Same for Windows folk who have WSL set up, or who can use GnuCOBOL with MSYS2.

With either compiler installed, you are now ready to start writing COBOL. The best part of this is that we can completely skip talking about the Job Control Language (JCL), which is an eldritch horror that one would normally be exposed to on IBM OS/360 systems and kin. Instead we can just use GCC (or GnuCOBOL) any way we like, including calling it directly on the CLI, via a Makefile or integrated in an IDE if that’s your thing.

Hello COBOL

As is typical, we start with the ‘Hello World’ example as a first look at a COBOL application:

IDENTIFICATION DIVISION.
    PROGRAM-ID. hello-world.
PROCEDURE DIVISION.
    DISPLAY "Hello, world!".
    STOP RUN.

Assuming we put this in a file called hello_world.cob, this can then be compiled with e.g. GnuCOBOL: cobc -x -free hello_world.cob.

The -x indicates that an executable binary is to be generated, and -free that the provided source uses free format code, meaning that we aren’t bound to specific column use or sequence numbers. We’re also free to use lowercase for all the verbs, but having it as uppercase can be easier to read.

From this small example we can see the most important elements, starting with the identification division with the program ID and optionally elements like the author name, etc. The program code is found in the procedure division, which here contains a single display verb that outputs the example string. Of note is the use of the period (.) as a statement terminator.

At the end of the application we indicate this with stop run., which terminates the application, even if called from a sub program.

Hello Data

As fun as a ‘hello world’ example is, it doesn’t give a lot of details about COBOL, other than that it’s quite succinct and uses plain English words rather than symbols. Things get more interesting when we start looking at the aspects which define this domain specific language, and which make it so relevant today.

Few languages support decimal (fixed point) calculations, for example. In this COBOL Basics project I captured a number of examples of this and related features. The main change is the addition of the data division following the identification division:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 A PIC 99V99 VALUE 10.11.
01 B PIC 99V99 VALUE 20.22.
01 C PIC 99V99 VALUE 00.00.
01 D PIC $ZZZZV99 VALUE 00.00.
01 ST PIC $*(5).99 VALUE 00.00.
01 CMP PIC S9(5)V99 USAGE COMP VALUE 04199.04.
01 NOW PIC 99/99/9(4) VALUE 04102034.

The data division is unsurprisingly where you define the data used by the program. All variables used are defined within this division, contained within the working-storage section. While seemingly overwhelming, it’s fairly easily explained, starting with the two digits in front of each variable name. This is the data level and is how COBOL structures data, with 01 being the highest (root) level, with up to 49 levels available to create hierarchical data.

This is followed by the variable name, up to 30 characters, and then the PICTURE (or PIC) clause. This specifies the type and size of an elementary data item. If we wish to define a decimal value, we can do so as two numeric characters (represented by 9) followed by an implied decimal point V, with two decimal numbers (99).  As shorthand we can use e.g. S9(5) to indicate a signed value with 5 numeric characters. There a few more special characters, such as an asterisk which replaces leading zeroes and Z for zero suppressing.

The value clause does what it says on the tin: it assigns the value defined following it to the variable. There is however a gotcha here, as can be seen with the NOW variable that gets a value assigned, but due to the PIC format is turned into a formatted date (04/10/2034).

Within the procedure division these variables are subjected to addition (ADD A TO B GIVING C.), subtraction with rounding (SUBTRACT A FROM B GIVING C ROUNDED.), multiplication (MULTIPLY A BY CMP.) and division (DIVIDE CMP BY 20 GIVING ST.).

Finally, there are a few different internal formats, as defined by USAGE: these are computational (COMP) and display (the default). Here COMP stores the data as binary, with a variable number of bytes occupied, somewhat similar to char, short and int types in C. These internal formats are mostly useful to save space and to speed up calculations.

Hello Business

In a previous article I went over the reasons why a domain specific language like COBOL cannot be realistically replaced by a general language. In that same article I discussed the Hello Business project that I had written in COBOL as a way to gain some familiarity with the language. That particular project should be somewhat easy to follow with the information provided so far. New are mostly file I/O, loops, the use of perform and of course the Report Writer, which is probably best understood by reading the IBM Report Writer Programmer’s Manual (PDF).

Going over the entire code line by line would take a whole article by itself, so I will leave it as an exercise for the reader unless there is somehow a strong demand by our esteemed readers for additional COBOL tutorial articles.

Suffice it to say that there is a lot more functionality in COBOL beyond these basics. The IBM ILE COBOL reference (PDF), the IBM Mainframer COBOL tutorial, the Wikipedia entry and others give a pretty good overview of many of these features, which includes object-oriented COBOL, database access, heap allocation, interaction with other languages and so on.

Despite being only a novice COBOL programmer at this point, I have found this DSL to be very easy to pick up once I understood some of the oddities about the syntax, such as the use of data levels and the PIC formats. It is my hope that with this article I was able to share some of the knowledge and experiences I gained over the past weeks during my COBOL crash course, and maybe inspire others to also give it a shot. Let us know if you do!

 

This articles is written by : Nermeen Nabil Khear Abdelmalak

All rights reserved to : USAGOLDMIES . www.usagoldmines.com

You can Enjoy surfing our website categories and read more content in many fields you may like .

Why USAGoldMines ?

USAGoldMines is a comprehensive website offering the latest in financial, crypto, and technical news. With specialized sections for each category, it provides readers with up-to-date market insights, investment trends, and technological advancements, making it a valuable resource for investors and enthusiasts in the fast-paced financial world.

Recent:

A New And Weird Kind of Typewriter Lewin Day | usagoldmines.com

FLOSS Weekly Episode 831: Let’s Have Lunch Jonathan Bennett | usagoldmines.com

Layout A PCB with Tscircuit Al Williams | usagoldmines.com

Supercon 2024: Photonics/Optical Stack for Smart-Glasses Lewin Day | usagoldmines.com

Radio Repeaters In the Sky Bryan Cockfield | usagoldmines.com

Terminal DAW Does it in Style Fenix Guthrie | usagoldmines.com

Building an nRF52840 and Battery-Powered Zigbee Gate Sensor Maya Posch | usagoldmines.com

Back to Reality with the Time Brick Bryan Cockfield | usagoldmines.com

Comparing ‘AI’ for Basic Plant Care With Human Brown Thumbs Maya Posch | usagoldmines.com

Read Motor Speed Better By Making The RP2040 PIO Do It Donald Papp | usagoldmines.com

Crossing Commodore Signal Cables on Purpose Bryan Cockfield | usagoldmines.com

There’s An Venusian Spacecraft Coming Our Way Jenny List | usagoldmines.com

The DIY 1982 Picture Phone Al Williams | usagoldmines.com

Peeking at Poking Health Tech: the G7 and the Libre 3 Heidi Ulrich | usagoldmines.com

Keebin’ with Kristina: the One with the Protractor Keyboard Kristina Panos | usagoldmines.com

Hydrogen Trains: Not The Success Germany Hoped They Would Be Jenny List | usagoldmines.com

Weird And Wonderful VR/MR Text Entry Methods, All In One Place Donald Papp | usagoldmines.com

Pi Pico Throws Us for a (MIDI) Loop Tyler August | usagoldmines.com

Deriving the Reactance Formulas Al Williams | usagoldmines.com

EclairM0, the pocket notepad Matt Varian | usagoldmines.com

Tinycorder Isn’t Quite a Tricorder, But… Al Williams | usagoldmines.com

Paint Mixing Theory for Custom Filament Colors Aaron Beckendorf | usagoldmines.com

Supercon 2024: Sketching With Machines Lewin Day | usagoldmines.com

X-Rays From an Overdriven Magnetron Dan Maloney | usagoldmines.com

Life on K2-18b? Don’t Get Your Hopes Up Just Yet Tom Nardi | usagoldmines.com

ASUS GPU Uses Gyroscope to Warn for Sagging Cards Maya Posch | usagoldmines.com

Look! It’s a Knob! It’s a Jack! It’s Euroknob! Dan Maloney | usagoldmines.com

Kaleidoscopico Shows Off Pi Pico’s Capabilities Bryan Cockfield | usagoldmines.com

Design Constraints Bring Lockbox to Life Bryan Cockfield | usagoldmines.com

Hackaday Links: April 27, 2025 Dan Maloney | usagoldmines.com

How Methane Took Over the Booster World Tyler August | usagoldmines.com

Hackers Create Fake Corporate Entities in the US To Fool Crypto Developers and Spread Malware: Repor...

Quick and Easy Digital Stethoscope Keeps Tabs on Cat Dan Maloney | usagoldmines.com

VESC Mods Made Via Vibe Coding Lewin Day | usagoldmines.com

Save Cells from the Landfill, Get a Power Bank For Your Troubles Donald Papp | usagoldmines.com

Deep Dive on Panel Making Al Williams | usagoldmines.com

Creating An Electronic Board For Catan-Compatible Shenanigans Lewin Day | usagoldmines.com

Another Coil Winder Project Al Williams | usagoldmines.com

YKK’s Self-Propelled Zipper: Less Crazy Than It Seems Maya Posch | usagoldmines.com

Remembering Heathkit Al Williams | usagoldmines.com

88,848 Americans Exposed As Massive Medical Data Breach Leaks Names, Addresses, Social Security Numb...

Wells Fargo Customer Loses $8,265 As Thieves Laugh, $28,000 Drained From JPMorgan Chase Account in A...

Quantum Random Number Generator Squirts Out Numbers Via MQTT Lewin Day | usagoldmines.com

Wells Fargo To Pay $185,000,000 To Customers in Massive New Settlement – Here’s Who Will Benefit Ale...

From Good Enough to Best Elliot Williams | usagoldmines.com

Digital Squid’s Behavior Shaped by Neural Network Bryan Cockfield | usagoldmines.com

Amazing Oscilloscope Demo Scores The Win At Revision 2025 Lewin Day | usagoldmines.com

RP2040 Spins Right ‘Round inside POV Display Tyler August | usagoldmines.com

Hash Functions with the Golden Ratio Bryan Cockfield | usagoldmines.com

7,605 Bank Customers Receive Urgent Data Breach Alerts After ‘Administrative Error’ Exposes Social S...

XOR Gate as a Frequency Doubler Al Williams | usagoldmines.com

Retired NBA Star Shaquille O’Neal Settles FTX Endorsement Lawsuit for Undisclosed Amount Rhodilee Je...

Robot Gets a DIY Pneumatic Gripper Upgrade Lewin Day | usagoldmines.com

Hackaday Podcast Ep 318: DIY Record Lathe, 360 Degree LIDAR, and 3D Printing Innovation Lives! Jenny...

Sigrok Website Down After Hosting Data Loss Maya Posch | usagoldmines.com

You Wouldn’t Steal a Font… Jenny List | usagoldmines.com

This Week in Security: XRP Poisoned, MCP Bypassed, and More Jonathan Bennett | usagoldmines.com

Posthumous Composition Being Performed by the Composer Seth Mabbott | usagoldmines.com

Clickspring’s Experimental Archaeology: Concentric Thin-Walled Tubing Dan Maloney | usagoldmines.com

Adding an Atari Joystick Port to TheC64 USB Joystick Lewin Day | usagoldmines.com

LLMs Coming for a DNA Sequence Near You Navarre Bartz | usagoldmines.com

3D Printing A Useful Fixturing Tool Lewin Day | usagoldmines.com

Onkyo Receiver Saved With An ESP32 Lewin Day | usagoldmines.com

DolphinGemma Seeks to Speak to Dolphins Navarre Bartz | usagoldmines.com

A Bicycle is Abandonware Now? Clever Hack Rescues Dead Light Jenny List | usagoldmines.com

From PostScript to PDF Al Williams | usagoldmines.com

Haptic Soft Buttons Speak(er) to Your Sense of Touch Tyler August | usagoldmines.com

The Mohmmeter: A Steampunk Multimeter Matt Varian | usagoldmines.com

C64 Assembly in Parts Al Williams | usagoldmines.com

Improved and Open Source: Non-Planar Infill for FDM Heidi Ulrich | usagoldmines.com

Abusing DuckDB-WASM To Create Doom In SQL Maya Posch | usagoldmines.com

The Evertop: a Low-Power, Off-Grid Solar Gem Heidi Ulrich | usagoldmines.com

FLOSS Weekly Episode 830: Vibes Jonathan Bennett | usagoldmines.com

Open Source Commercial Synthesisers You Will Love Jenny List | usagoldmines.com

To See Within: Detecting X-Rays Dan Maloney | usagoldmines.com

Unsolved Questions in Astronomy? Try Dark Matter! Tyler August | usagoldmines.com

A Scratch-Built Commodore 64, Turing Style Dan Maloney | usagoldmines.com

Virtual Nodes, Real Waves: a Colpitts Walkthrough Heidi Ulrich | usagoldmines.com

How Supercritical CO2 Working Fluid Can Increase Power Plant Efficiency Maya Posch | usagoldmines.co...

eInk PDA Revisited Fenix Guthrie | usagoldmines.com

DIY Record Cutting Lathe is Really Groovy Tyler August | usagoldmines.com

British Wartime Periscope: a Peek Into the Past Heidi Ulrich | usagoldmines.com

Game Boy PCB Assembled With Low-Cost Tools Bryan Cockfield | usagoldmines.com

27% of Bybit Hacked Funds Have ‘Gone Dark’ After Flowing Through Mixers and Bridges, According to CE...

Why Physical Media Deserved To Die Maya Posch | usagoldmines.com

What’s Sixty Feet Across and Superconducting? Tyler August | usagoldmines.com

Making A One-Of-A-Kind Lime2 SBC Matt Varian | usagoldmines.com

Making Your Own Light Bulb Using a Jar, a Pencil, and Two Bolts John Elliot V | usagoldmines.com

PoX: Super-Fast Graphene-Based Flash Memory Maya Posch | usagoldmines.com

Jolly Wrencher Down to the Micron Ian Bos | usagoldmines.com

Trekulator: A Reproduction of the 1977 Star Trek Themed Calculator John Elliot V | usagoldmines.com

Remembering UCSD p-System, the Pascal Virtual Machine Maya Posch | usagoldmines.com

Keebin’ with Kristina: the One with the Part Picker Kristina Panos | usagoldmines.com

Crypto Rug Pull Losses Have Soared 6,499% This Year Despite Decrease in Frequency, Says DappRadar Rh...

Restoration of Six-Player Arcade Game From the Early 90s Bryan Cockfield | usagoldmines.com

Biasing Transistors with Current Sources John Elliot V | usagoldmines.com

Printed Perpetual Calendar Clock Contains Clever Cams Tyler August | usagoldmines.com

Preventing Galvanic Corrosion in Water Cooling Loops Maya Posch | usagoldmines.com

China Hosts Robot Marathon Al Williams | usagoldmines.com

Hackaday Links: April 20, 2025 Dan Maloney | usagoldmines.com

Leave a Reply