Due to a new project at work, I'm starting to learn COBOL (not the sexiest language, I know). I found the hardest part about learning COBOL for me is to know what COBOL keywords mean in terms of more modern programming languages. Therefore, I thought I’ll write a couple of short tutorials here to explain some of these differences in case some other programmers are interested in learning this 50+ years old programming language.
As with learning any other programming language, the first example has to be the “Hello World” and here’s the source code:
IDENTIFICATION DIVISION. PROGRAM-ID. HELLOWORLD. PROCEDURE DIVISION. DISPLAY 'HELLO WORLD'. STOP RUN.
Every COBOL program needs an IDENFICATION DIVISION
and the PROGRAM-ID
(which is HELLOWORLD
in our example). All program logic will sit under the PROCEDURE DIVISION
. The rest of the program should be pretty self-explanatory.
The full stop (.) is the equivalent of semi-colon (;) in C-derived programming languages, which denotes the end of a coding line.
To compile this code, I used the OpenCOBOL compiler. You can install it under Ubuntu 10.04 by typing the following command in the shell:
sudo apt-get install open-cobol
After installing the compiler, you can then compile the program by running (assuming that you’ve saved the source code in a file called helloworld.cob
):
cobc -x -free helloworld.cob
The -free
compiler flag tells the cobc
compiler to use the free source code format. Without it, the compiler will require you to enter 7 spaces at the beginning of each line. The –x
flag, on the other hand, tells the compiler to produce an executable rather than a .so
file (we’ll talk about .so files later).
Finally, the compiler may produce few warnings about “dereferencing type-punned pointer” but you can just ignore them. After the compilation finishes, you will find the executable helloworld
in your directory.
Comments
Post a Comment