Beginner

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:

1
2
3
4
5
6
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

PROCEDURE DIVISION.
    DISPLAY "Hello, World!".
    STOP RUN.

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 program
  • PROGRAM-ID. HELLO-WORLD. - Names the program (must be unique)
  • PROCEDURE DIVISION. - Contains the executable code
  • DISPLAY "Hello, World!". - Outputs text to the console
  • STOP 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Create the source file
cat > hello.cob << 'EOF'
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

PROCEDURE DIVISION.
    DISPLAY "Hello, World!".
    STOP RUN.
EOF

# Compile and run with GnuCOBOL
docker run --rm -v $(pwd):/app -w /app esolang/cobol sh -c 'cobc -x -free hello.cob && ./hello'

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:

1
2
3
4
5
# Compile
cobc -x -free hello.cob

# Run
./hello

Installing GnuCOBOL

macOS (Homebrew):

1
brew install gnu-cobol

Ubuntu/Debian:

1
sudo apt install gnucobol

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR. YOUR-NAME.
DATE-WRITTEN. 2025-12-24.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. MODERN-PC.
OBJECT-COMPUTER. MODERN-PC.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 GREETING PIC X(20) VALUE "Hello, World!".

PROCEDURE DIVISION.
    DISPLAY GREETING.
    STOP RUN.

The Four Divisions Explained

  1. IDENTIFICATION DIVISION

    • Required in every program
    • Contains metadata: program name, author, date
    • Only PROGRAM-ID is mandatory
  2. ENVIRONMENT DIVISION

    • Describes hardware and file configuration
    • Optional in simple programs
    • Specifies source/object computers
  3. DATA DIVISION

    • Defines all variables and data structures
    • WORKING-STORAGE for internal variables
    • FILE SECTION for external files
  4. 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)

1
2
3
4
5
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. HELLO-WORLD.
000300 PROCEDURE DIVISION.
000400     DISPLAY "Hello, World!".
000500     STOP RUN.

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)

1
2
3
4
5
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
    DISPLAY "Hello, World!".
    STOP RUN.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-MESSAGE PIC X(30) VALUE "Hello, World!".

PROCEDURE DIVISION.
    DISPLAY WS-MESSAGE.
    STOP RUN.

With Full Metadata

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
AUTHOR. GRACE-HOPPER.
DATE-WRITTEN. 2025-12-24.
REMARKS. DEMONSTRATING COBOL BASICS.

PROCEDURE DIVISION.
MAIN-PARAGRAPH.
    DISPLAY "Hello, World!".
    STOP RUN.

Key Concepts

  1. COBOL is compiled - Source code (.cob) is compiled to a native executable
  2. Division structure is mandatory - Every program needs at least IDENTIFICATION and PROCEDURE divisions
  3. Periods are statement terminators - Don’t forget the periods!
  4. Readable by design - Code should read like business documentation
  5. Case insensitive - DISPLAY, Display, and display are identical

Compiler Options

Useful GnuCOBOL flags for development:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Create executable with debugging symbols
cobc -x -free -g -debug hello.cob

# Compile with warnings
cobc -x -free -Wall hello.cob

# List all divisions and sections (verbose)
cobc -x -free -v hello.cob

# Create object file (no linking)
cobc -c -free hello.cob

Common Beginner Mistakes

Forgetting Periods

1
2
DISPLAY "Hello, World!"    Missing period
DISPLAY "Hello, World!".   Correct

Wrong Division Order

1
2
3
4
5
PROCEDURE DIVISION.         Wrong - PROCEDURE before IDENTIFICATION
IDENTIFICATION DIVISION.

IDENTIFICATION DIVISION.    Correct order
PROCEDURE DIVISION.

Incorrect Indentation in Fixed Format

If not using -free, respect column rules:

1
2
DISPLAY "Hello, World!".    Starts in column 1
    DISPLAY "Hello, World!".   Starts in Area B (column 12+)

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
Last updated: