COBOL Get Started
You can run every example in this course right here in the browser: the code is sent to Compiler Explorer, compiled with GnuCOBOL 3.2 and executed, and the output appears below the editor. To work on your own computer you only need a COBOL compiler and a text editor.
Installing GnuCOBOL
| System | Command |
|---|---|
| Debian / Ubuntu | sudo apt install gnucobol |
| Fedora | sudo dnf install gnucobol |
| macOS (Homebrew) | brew install gnucobol |
| Windows | Use WSL with the Ubuntu package, or a prebuilt GnuCOBOL installer |
Check the installation:
$ cobc --version
cobc (GnuCOBOL) 3.2.0Your first program
Save the following as hello.cob. Notice the seven spaces at the start of every line: in COBOL's traditional
fixed format, code starts in column 8. The next lesson but one explains why.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, World!"
STOP RUN.Hello, World!
Compile and run it in a terminal:
$ cobc -x hello.cob # -x builds an executable named "hello"
$ ./hello
Hello, World!cobc option |
Meaning |
|---|---|
-x |
Build an executable program (with a main entry point) |
-m |
Build a dynamically loadable module, used for subprograms called with CALL |
-free |
Accept free-format source (no column rules) |
-Wall |
Show all warnings |
-std=ibm |
Behave like IBM Enterprise COBOL (other dialects: mf, cobol2014, …) |
What each line does
IDENTIFICATION DIVISION.starts the program and is required.PROGRAM-ID. HELLO.names the program. The name is used when other programsCALLit.PROCEDURE DIVISION.starts the executable statements.DISPLAYwrites its operands to standard output, followed by a newline.STOP RUN.ends the program. The final period ends the sentence.
Displaying several items
DISPLAY accepts any number of literals and data items and prints them side by side, with no separator.
Add spaces inside the literals yourself:
IDENTIFICATION DIVISION.
PROGRAM-ID. GREETING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(5) VALUE "Grace".
01 WS-YEAR PIC 9(4) VALUE 1959.
PROCEDURE DIVISION.
DISPLAY "Hello, " WS-NAME "!"
DISPLAY "COBOL was designed in " WS-YEAR "."
STOP RUN.Hello, Grace!
COBOL was designed in 1959.
WS-NAME and WS-YEAR are data items declared in the WORKING-STORAGE SECTION. The WS- prefix is a
common naming convention meaning "working storage"; it is not required by the language.
Compile errors
When something is wrong, cobc reports the line number and a message. The program below misspells DISPLAY:
IDENTIFICATION DIVISION.
PROGRAM-ID. TYPO.
PROCEDURE DIVISION.
DISPLAYY "Oops"
STOP RUN.<source>:5: warning: line not terminated by a newline [-Wmissing-newline]
3 | PROCEDURE DIVISION.
4 | DISPLAYY "Oops"
5 > STOP RUN.<EOF>
<source>:4: error: syntax error, unexpected Literal, expecting SECTION or .
2 | PROGRAM-ID. TYPO.
3 | PROCEDURE DIVISION.
4 > DISPLAYY "Oops"
5 | STOP RUN.<EOF>
<source>:4: note: unknown statement 'DISPLAYY'
GnuCOBOL shows each message with the surrounding source lines and marks the offending line with >. The
first message is only a warning (the playground sends the source without a final newline); the real problem is the
error on line 4, and the note explains that DISPLAYY is not a known statement.
Tip: COBOL error messages sometimes point at the line after the real mistake, because the compiler only notices the problem when the next word does not fit. A missing period is the classic case. Always look one line up as well.
Test yourself
Which cobc option builds a runnable executable?
- -m
- -x
- -free
- -run
In fixed format, in which column does code such as IDENTIFICATION DIVISION. start?
- Column 1
- Column 7
- Column 8
- Column 12
What does DISPLAY "A" "B" print?
- A B
- AB
- A, B
- A on one line and B on the next