Hello World in COBOL
Your first COBOL program - the classic Hello World example with Docker setup using GnuCOBOL
Every programming journey starts with Hello World. Let’s write our first COBOL program using modern free-format syntax with GnuCOBOL.
The Code
Create a file named hello.cob:
| |
Understanding the Code
COBOL programs follow a structured format with four main divisions. Our simple Hello World uses two:
IDENTIFICATION DIVISION.- Required division that identifies the programPROGRAM-ID. HELLO-WORLD.- Names the program (must be unique)PROCEDURE DIVISION.- Contains the executable codeDISPLAY "Hello, World!".- Outputs text to the consoleSTOP RUN.- Terminates the program execution
The COBOL Philosophy
Notice the English-like syntax? COBOL was designed in 1959 to be readable by business managers, not just programmers. Every statement reads like a sentence, and periods mark the end of sentences.
Why All Caps?
Traditionally, COBOL code was written in ALL CAPS because early computers only supported uppercase. Modern COBOL compilers (including GnuCOBOL) are case-insensitive, but the ALL CAPS convention persists in most production code. For this tutorial, we’ll follow tradition.
Running with Docker
The easiest way to run COBOL without installing a compiler locally uses GnuCOBOL in a Docker container:
| |
Understanding the Docker Command
cobc- The GnuCOBOL compiler-x- Create an executable program-free- Use free-format source (no column restrictions)hello.cob- Input source file./hello- Run the compiled executable
Running Locally
If you have GnuCOBOL installed:
| |
Installing GnuCOBOL
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
Windows: Download from GnuCOBOL for Windows.
Expected Output
Hello, World!
Clean and simple - exactly what you’d expect!
The Complete Four-Division Structure
While our Hello World is minimal, a typical COBOL program uses all four divisions:
| |
The Four Divisions Explained
IDENTIFICATION DIVISION
- Required in every program
- Contains metadata: program name, author, date
- Only PROGRAM-ID is mandatory
ENVIRONMENT DIVISION
- Describes hardware and file configuration
- Optional in simple programs
- Specifies source/object computers
DATA DIVISION
- Defines all variables and data structures
- WORKING-STORAGE for internal variables
- FILE SECTION for external files
PROCEDURE DIVISION
- Contains the executable logic
- Organized into paragraphs and sections
- Where the actual work happens
Fixed vs. Free Format
COBOL has two source formats:
Fixed Format (Classic Style)
| |
Fixed format requires:
- Columns 1-6: Sequence numbers
- Column 7: Indicator (comment, continuation)
- Columns 8-11: Area A (division, section headers)
- Columns 12-72: Area B (statements)
- Columns 73-80: Ignored (originally for program ID)
Free Format (Modern Style)
| |
Free format (enabled with -free flag):
- No column restrictions
- More flexible indentation
- Easier to edit in modern editors
- Recommended for new code
For this tutorial series, we’ll use free format, but you’ll encounter fixed format in legacy codebases.
Alternative Hello World Styles
COBOL’s verbosity allows many variations:
Using Working Storage
| |
With Full Metadata
| |
Key Concepts
- COBOL is compiled - Source code (
.cob) is compiled to a native executable - Division structure is mandatory - Every program needs at least IDENTIFICATION and PROCEDURE divisions
- Periods are statement terminators - Don’t forget the periods!
- Readable by design - Code should read like business documentation
- Case insensitive -
DISPLAY,Display, anddisplayare identical
Compiler Options
Useful GnuCOBOL flags for development:
| |
Common Beginner Mistakes
Forgetting Periods
| |
Wrong Division Order
| |
Incorrect Indentation in Fixed Format
If not using -free, respect column rules:
| |
A Bit of History
When COBOL was created in 1959, Grace Hopper’s team designed it to be radically different from FORTRAN and other languages of the time. The goal: make programming accessible to business professionals, not just mathematicians and engineers.
This simple Hello World program would have been compiled on massive mainframes that filled entire rooms, punched onto cards, and submitted as batch jobs. The fact that we can now run the same language in a Docker container on a laptop demonstrates COBOL’s remarkable portability and longevity.
Why Learn COBOL Today?
While COBOL may seem archaic, it offers unique career opportunities:
- High demand, low supply - Organizations desperately need COBOL programmers
- Premium salaries - COBOL skills command competitive compensation
- Mission-critical systems - Work on systems processing trillions in transactions
- Job security - Legacy systems aren’t going anywhere soon
- Historical perspective - Understand the foundations of business computing
Next Steps
Continue to Variables and Data Types to learn about COBOL’s powerful data definition capabilities.
Running Today
All examples can be run using Docker:
docker pull esolang/cobol:latest