What COBOL is and where it runs
COBOL is a compiled, statically typed language for business data processing. It was designed in 1959 by a committee that wanted a language a manager could read, and the decisions of that committee are still visible in every program: a field is declared with its exact width before it is used, arithmetic is decimal, and a sentence ends with a period.
This lesson is the tour before the work: one program that runs, the binary the compiler made of it, the machines the language lives on, and the one property that explains why banks still keep it, which is exact decimal money. Everything you see was compiled and run on the machine named at the end of the page.
A first program
A COBOL program is a sequence of divisions, and the smallest useful program has two: IDENTIFICATION DIVISION names it, PROCEDURE DIVISION holds the statements. DISPLAY writes a line to standard output and STOP RUN ends the run unit.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, COBOL".
STOP RUN.
Every line here is free format: no columns, no sequence numbers, and an *> starts a comment. GnuCOBOL accepts this form with -free, which is the form this whole course uses, and the lesson on free and fixed format shows the older column layout of the same program.
cobc -x -free -Wall -o hello hello.cob
./hello
echo "status: $?"
Hello, COBOL
status: 0
The compiler printed nothing, which means no warning and no error. -Wall turns on the warning groups that GnuCOBOL keeps apart from its default set, so a clean build here is a real statement about the program; the lesson on warnings and errors shows a program that does not build cleanly.
What the compiler made
cobc is not an interpreter with a launcher. GnuCOBOL translates COBOL to C, compiles that C with the system C compiler, and links a native executable. The result is a file like any other program on the machine, and file describes it without mentioning COBOL at all.
file hello
ls -l hello
cobc --version
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, BuildID[sha1]=cf329806291bb94f1ecd5cf2dbae1504d573f86b, with debug_info, not stripped
-rwxr-xr-x 1 agent agent 19384 Nov 18 05:50 hello
cobc (GnuCOBOL) 3.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built May 23 2026 15:18:55
Packaged Jul 28 2023 17:02:56 UTC
C version "15.2.0"
The binary is 19 kilobytes and it needs the C library of the system, as the interpreter line shows. The last line of the version output is the reason: a COBOL program really is a C program after the compiler has finished with it, and the compiler that finishes the job is the gcc of this machine, here version 15.2.0.
Where COBOL runs
The language is standardized by ISO, and the standard is what makes the source portable: the same record description and the same PERFORM loop are valid on an IBM mainframe under z/OS, on a Unix server under Micro Focus, and on a laptop under GnuCOBOL. What changes is the compiler, the file system and the way a job is started, not the language.
Commercial COBOL lives on the mainframe and on large Unix machines, and the work is batch and transaction processing: payroll runs at night, account postings during the day, insurance policies, airline reservations, tax records. The programs are long-lived because the business rules in them are long-lived, and because a record format that a hundred programs share cannot be changed by one team on a Tuesday.
GnuCOBOL is the free compiler used on this site. It implements the 1985 standard, most of the 2002 and 2014 additions, and it runs on Linux, macOS, Windows and the BSDs. The install guides cover each system, and the mainframe page describes what a z/OS job looks like, which this container does not run.
Money is decimal
The one design decision worth knowing before anything else: a COBOL number is decimal and its scale is part of its declaration. PIC 9(3)V99 is three integer digits and two decimal places, stored exactly, and the arithmetic verbs work on those digits. No binary fraction is involved, so a cent is a cent.
The program below multiplies a quantity by a price and prints the total, then adds 0.1 and 0.2. A binary floating point type would answer the second sum with something like 0.30000000000000004; COBOL answers with the digits it was told to keep.
IDENTIFICATION DIVISION.
PROGRAM-ID. INVOICE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-QTY PIC 9(3) VALUE 3.
01 WS-PRICE PIC 9(3)V99 VALUE 19.99.
01 WS-TOTAL PIC 9(5)V99.
01 WS-TENTH PIC 9V9 VALUE 0.1.
01 WS-SUM PIC 9V9.
PROCEDURE DIVISION.
COMPUTE WS-TOTAL = WS-QTY * WS-PRICE.
DISPLAY "total: " WS-TOTAL.
ADD 0.2 TO WS-TENTH GIVING WS-SUM.
DISPLAY "0.1 + 0.2 = " WS-SUM.
STOP RUN.
cobc -x -free -Wall -o invoice invoice.cob
./invoice
total: 00059.97
0.1 + 0.2 = 0.3
DISPLAY shows the field as it is stored: five integer digits, a point, two decimals, so the total has leading zeros. The lesson on edited numeric output turns that into a printed amount, and the lesson on decimal money puts the same sum beside a binary float and shows what a float loses.
The edited picture below is COBOL's currency format: the dollar sign, the thousands separator and two decimals, filled from the same value.
IDENTIFICATION DIVISION.
PROGRAM-ID. MONEY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AMOUNT PIC 9(5)V99 VALUE 1234.5.
01 WS-EDITED PIC $ZZ,ZZ9.99.
PROCEDURE DIVISION.
MOVE WS-AMOUNT TO WS-EDITED.
DISPLAY WS-EDITED.
STOP RUN.
cobc -x -free -Wall -o money money.cob
./money
$ 1,234.50
An edited field is still a character field: the picture describes what the reader sees, not what the arithmetic works on. Keeping the two apart is the habit the course builds from lesson 11 onwards.
The tools of the course
One compiler, one command, and the C compiler that the system already has. cobc -x builds an executable, cobc -m builds a module that another program calls, and cobc -fsyntax-only checks a source file without writing anything. The course uses free format (-free) everywhere and turns the warning groups on (-Wall) in every build.
There is no COBOL linter in this environment: no cobol-check, no zUnit, no GnuCOBOL test framework. What a page can promise is what was run here, and that is the compiler, the program, and its output. The next lesson installs the compiler on your own machine.
Exercises
- Change the greeting in hello.cob and run the build again. What does the shell print for $? and why is it that number?
- Add a comment line that starts with *> between the two divisions and confirm that the build still succeeds.
- Make invoice.cob print the total with four decimal places by widening WS-TOTAL and the picture of the price. Which digits are now printed and which are still missing?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which two divisions does the smallest runnable COBOL program need?
Answer
IDENTIFICATION DIVISION and PROCEDURE DIVISION
The program must be named and it must have statements; the other two divisions are optional until a program describes data or a file.
-
What does
cobc -x -freeproduce?Answer
A native executable for this machine
-x asks for an executable: cobc writes C, compiles it with the system C compiler and links the result.
-
What does
-Walldo incobc -x -free -Wall -o hello hello.cob?Answer
It turns on warning groups that are off by default
-Wall enables the extra warning groups; -Werror, which turns warnings into errors, is a different option.
-
Why does COBOL store money as decimal digits instead of a binary float?
Answer
Because the scale is declared, so a cent is stored and added exactly
PIC 9(3)V99 fixes the scale at declaration time, so every value and every sum is an exact number of cents.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. PROCEDURE DIVISION. DISPLAY "one". DISPLAY "two". STOP RUN.Answer
one / two
Each DISPLAY writes a line of its own, and the checker joins the lines with " / ".
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 7. 01 B PIC 9(2) VALUE 5. 01 C PIC 9(3). PROCEDURE DIVISION. COMPUTE C = A * B. DISPLAY C. STOP RUN.Answer
035
PIC 9(3) holds three digits, and DISPLAY prints the field as stored, leading zero included.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 P PIC 9(2)V99 VALUE 1.50. 01 Q PIC 9(2) VALUE 2. 01 T PIC 9(4)V99. PROCEDURE DIVISION. COMPUTE T = P * Q. DISPLAY T. STOP RUN.Answer
0003.00
T has four integer digits and two decimals, so the exact product 3.00 is printed as 0003.00.
-
What does this program print, and what status does the shell see?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. PROCEDURE DIVISION. DISPLAY "done". STOP RUN.Answer
done / status: 0
STOP RUN ends the run unit normally, which is exit status 0.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. PROCEDURE DIVISION. DISPLAY "one" SECOND. DISPLAY "two".Answer
error: 'SECOND' is not defined
Without the period that ends the sentence, cobc reads SECOND as the name of a verb and looks it up as a statement.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9(2) VALUE 100. PROCEDURE DIVISION. DISPLAY N. STOP RUN.Answer
warning: value size exceeds data size [-Wothers]
The literal is truncated at compile time and cobc prints a warning, not an error, so the build still succeeds.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev