Your first COBOL program
| The source file | |
| Compile it | |
| Run it | |
| Anatomy |
The source file
COBOL traditionally uses fixed format: columns 1-6 are sequence numbers, column 7 is the indicator area, and code starts at column 8. GnuCOBOL also accepts free format with the -free flag, which this course uses.
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello from COBOL".
STOP RUN.
Compile it
Compile the file with the -x flag so you get an executable, then run it:
cobc -x -free -o hello hello.cob
./hello
Run it
The terminal prints the greeting and exits cleanly:
Anatomy
IDENTIFICATION DIVISION names the program. PROCEDURE DIVISION holds the logic. DISPLAY writes a line to the screen, and STOP RUN ends the program. The four-division structure never changes, from a five-line greeting to a million-line banking system.
Next: The four divisions
Article author: Arthur Isaev