The Smallest COBOL Program
Traditional Format
Traditionally, programming languages use the challenge of having printing “Hello World” on the console to demonstrate the simplest program possible in that language. For COBOL we offer two examples. The first, in old-school fixed format (and in all caps because that’s all keypunches could do):
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "HELLO WORLD".
When saved as hello-world.cob and compiled with:
cobc -x hello-world.cob
And then run with:
./hello-world
It produces HELLO WORLD just as you would like.
Modern Free Format
For the more modern free format of cobol the code looks like:
identification division.
program-id. hello-world.
procedure division.
display "Hello World".
This time we compile it with:
cobc -free -x hello-world.cob
And run it with:
./hello-world
And get “Hello World” as we should.
Shortcut to Compiling with cobc
When trying to compile the hello-world.cob, there is also the option of running the program directly after the build and shortcutting the two steps previously mentioned. This can be seen in the following example:
cobc -j hello-world.cob
The -j tells the compiler to go ahead and compile the program, and after the compiling is successful, to run the program and present the output.
Humorous Footnote
As a joke, here’s hellojoke.cob, the “one-liner” version:
id division. program-id. hello-world. proc division. display "Hello World".
It compiles the same and produces the same output.