Your first COBOL program

Every COBOL program in this course has the same skeleton: divisions in a fixed order, a name, and a procedure that does the work. This lesson writes the smallest one that is still a real program, compiles it, runs it, and then breaks it in the way a beginner breaks it first, so that the diagnostic is not a surprise later.

The program

Save this as hello.cob. The blank line is not required; it separates the identification from the procedure, which is the layout every later lesson keeps.

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAY "Hello, COBOL". STOP RUN.

Six lines, four of them structural. Two divisions: one names the program, one holds the statements. The statements are indented by hand; the compiler does not care about the indentation, and a reader does.

Build and run it

-x asks for an executable, -free says the source is free format, and -Wall turns on the extra warning groups. -o hello names the output, so the executable is hello whether or not the source is called hello.cob.

cobc -x -free -Wall -o hello hello.cob
./hello
echo "status: $?"

Hello, COBOL
status: 0

The build printed nothing, and silence from cobc with -Wall means no warning at all. The status is zero because the program ended with STOP RUN, which is a normal end of the run unit.

The lines one by one

IDENTIFICATION DIVISION. starts the first division. A division header is a word, the word DIVISION, and a period. It comes first, always, and every later division follows the order the standard fixes.

PROGRAM-ID. HELLO. names the program. The name is the one a CALL statement uses later, and the one cobcrun looks for when the program is built as a module. A hyphen is allowed in the name, a space is not: the period after the name ends the sentence, and the name itself is a single word.

PROCEDURE DIVISION. starts the part the machine runs. Everything before it describes the program; everything after it is a sequence of sentences, each ended by a period.

DISPLAY "Hello, COBOL". is a statement: a verb and its operands. It writes the literal to standard output and adds a newline. Two DISPLAY statements print two lines, in the order they are written, which the program below shows.

IDENTIFICATION DIVISION. PROGRAM-ID. LINES. PROCEDURE DIVISION. DISPLAY "first". DISPLAY "second". STOP RUN.

cobc -x -free -Wall -o lines lines.cob
./lines

first
second

DISPLAY is not print with formatting: it writes the storage of each operand, which is why a numeric field appears with its leading zeros and an alphanumeric one keeps its trailing spaces. The lesson on ACCEPT and DISPLAY takes the statement apart.

STOP RUN and the end of the program

STOP RUN ends the run unit and returns control to the operating system. A program without it also ends, at the end of PROCEDURE DIVISION, and the status is the same: zero unless RETURN-CODE was set.

IDENTIFICATION DIVISION. PROGRAM-ID. ENDING. PROCEDURE DIVISION. DISPLAY "no stop run".

cobc -x -free -Wall -o ending end.cob
./ending
echo "status: $?"

no stop run
status: 0

The difference matters in a subprogram, not in a main program: a subprogram that ends by falling off the end returns to its caller, and one that runs STOP RUN ends the whole run unit, caller included. The lesson on CALL and USING shows both.

A misspelled verb

Change DISPLAY to DISPLAYY and build again. The compiler does not guess: it reads the unknown word as the start of a statement, finds a literal where it expected the rest of a statement, and then tells you in a second line what it thinks you meant.

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO. PROCEDURE DIVISION. DISPLAYY "Hello, COBOL". STOP RUN.

cobc -x -free -Wall -o hello_broken hello_broken.cob

hello_broken.cob:5: error: syntax error, unexpected Literal, expecting SECTION or .
3 |
4 | PROCEDURE DIVISION.
5 > DISPLAYY "Hello, COBOL".
6 | STOP RUN.
7 | <EOF>
hello_broken.cob:5: note: unknown statement 'DISPLAYY'

Two things to read. The position is 5, the line with the mistake, and the caret line points into it. The note is the useful half: cobc recognised the word as a statement name it does not have. The first line is the error, and the shell sees status 1; the next lesson reads diagnostics of this shape in full.

Exercises

  1. Add a third DISPLAY to lines.cob and predict the three output lines before you build it.
  2. Rename the program to MY-HELLO and build it. Does the executable name change, and does the program name appear anywhere in the output?
  3. Fix the broken program by changing one letter, then build it with the same command and run it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which division holds the statements that run?

    Answer

    PROCEDURE DIVISION

    The PROCEDURE DIVISION is the executable part; the others describe the program, its data and its environment.

  2. What does the period after PROGRAM-ID. HELLO. do?

    Answer

    It ends the sentence that declares the program name

    A period ends a sentence in COBOL, so PROGRAM-ID and the name before the period form one sentence.

  3. What does -o hello change in cobc -x -free -Wall -o hello hello.cob?

    Answer

    The name of the executable that is written

    -o names the output file; the PROGRAM-ID in the source is untouched by it.

  4. What happens to a main program that has no STOP RUN?

    Answer

    It ends at the end of PROCEDURE DIVISION with status 0

    Reaching the end of the procedure ends the run unit normally, exactly as STOP RUN does in a main program.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. PROCEDURE DIVISION. DISPLAY "Hello, COBOL". STOP RUN.

    Answer

    Hello, COBOL

    DISPLAY writes the literal exactly as it is written, with its punctuation and its case.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. PROCEDURE DIVISION. DISPLAY "first". DISPLAY "second". STOP RUN.

    Answer

    first / second

    DISPLAY adds a newline after each operand, so the checker shows two lines joined with " / ".

  7. What does this program print, and what status does the shell see? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. PROCEDURE DIVISION. DISPLAY "end".

    Answer

    end / status: 0

    The statement runs and the procedure ends at its last line, which is a normal exit with status 0.

  8. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. PROCEDURE DIVISION. DISPLAYY "hi". STOP RUN.

    Answer

    error: syntax error, unexpected Literal, expecting SECTION or .

    cobc reads the unknown word as the start of a statement and reports the literal that follows it.

  9. Which name is legal for a COBOL program?

    Answer

    MY-PROGRAM

    A program name is one word, and a hyphen joins the parts; a space would end the word and a period would end the sentence.

  10. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 N PIC 9(3) VALUE 42. PROCEDURE DIVISION. DISPLAY "n=" N. STOP RUN.

    Answer

    n=042

    DISPLAY writes the operands one after another with no separator, and PIC 9(3) stores three characters, so 42 is 042.

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