Hello World (first programs) · Beginner · 16 min
Hello World in COBOL with GnuCOBOL: divisions, DISPLAY and ACCEPT
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · GnuCOBOL 3.2 (package gnucobol3)
You will install GnuCOBOL 3.2 on Ubuntu 26.04, compile and run a fixed-format and a free-format Hello World, see the C code it is translated into, and build a program that reads your name and adds money without rounding errors.
On this page
COBOL dates from 1959, and it is still running. Banks, insurers, pension funds, tax offices and airlines across the world process payments, policies and benefits with COBOL programs, many of them on IBM mainframes and many written decades ago. Those systems need people who can read them, and learning the basics takes an afternoon.
Three things make COBOL different from the languages you may know. Its statements read like English sentences (ADD 1 TO COUNTER, MOVE TOTAL TO REPORT-LINE), and they end with full stops. Every program is split into named divisions in a fixed order. And its numbers are decimal by design: you declare how many digits sit before and after the decimal point, so money adds up to the exact cent.
You use GnuCOBOL, a free compiler. Its cobc command translates COBOL into C and then hands that C to GCC, which produces an ordinary Linux program. You will look at that C code in this tutorial. Everything happens in a throwaway folder, ~/hello-cobol-demo, which you delete at the end.
Step 1 · Install GnuCOBOL
Ubuntu 26.04 packages GnuCOBOL 3.2 as gnucobol3 in the universe repository. Installing it also pulls in GCC, which cobc needs:
sudo apt update
sudo apt install -y gnucobol3
cobc --versionOn Windows, install Ubuntu under WSL and follow these commands. On macOS with Homebrew, run brew install gnucobol. The rest of the tutorial is the same.
GCC itself now has a COBOL compiler too: gcobol, added in GCC 15 and packaged on Ubuntu 26.04 as gcobol-15. It is newer and aims at programs written for IBM mainframes. This tutorial uses GnuCOBOL because it has been developed for longer and is widely used for learning and for running COBOL off the mainframe.
Check it worked
The first line of cobc --version is something like cobc (GnuCOBOL) 3.2.0. If you see Unable to locate package gnucobol3, the universe repository is turned off; run sudo add-apt-repository universe, then the two apt lines again.
Step 2 · Write and compile Hello World
Make the demo folder and write the program. The spaces at the start of each line matter, so copy them exactly: the first three lines start with 7 spaces, the last two with 11.
mkdir -p ~/hello-cobol-demo
cd ~/hello-cobol-demo
cat > hello.cob <<'COB'
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, world!".
STOP RUN.
COBWhat each line does:
IDENTIFICATION DIVISION.opens the first of COBOL's four divisions. It says who the program is. The other three are ENVIRONMENT (files and machines), DATA (variables) and PROCEDURE (the code). You may leave out divisions you do not need, but the ones you write must come in that order.PROGRAM-ID. HELLO.names the program. Other COBOL programs use this name to call it.PROCEDURE DIVISION.starts the instructions.DISPLAY "Hello, world!".writes the text and a newline to the screen. The full stop at the end closes the sentence.STOP RUN.ends the program and returns control to the operating system.
Keywords are not case-sensitive. Upper case is tradition, from the days of punched cards and terminals without lower case.
Now compile and run it:
cd ~/hello-cobol-demo
cobc -x hello.cob
./hello-x tells cobc to build an executable program. The output is named after the source file, so you get hello. ./hello runs it from the current folder.
Check it worked
cobc prints nothing when the program compiles cleanly. ./hello prints Hello, world!. ls shows hello next to hello.cob.
Step 3 · Understand fixed format and switch to free format
Those leading spaces are the part of COBOL that catches everyone out. By default cobc reads source in fixed format, a layout inherited from 80-column punched cards, where the column a character sits in changes its meaning:
- Columns 1 to 6, the sequence area. Card numbers, so a dropped deck could be put back in order. The compiler ignores them. Old code often has numbers like
000100here. - Column 7, the indicator area. Blank for normal code. A
*makes the line a comment, and a-continues a long text value from the line before. - Columns 8 to 11, area A. Division, section and paragraph names start here, as do top-level data items (level
01). - Columns 12 to 72, area B. Statements such as
DISPLAYandSTOP RUNgo here. - Columns 73 to 80. An identification area, also ignored. Text you type past column 72 is not part of the program.
That is why the division lines start with 7 spaces (they begin in column 8, area A) and the statements with 11 (column 12, area B). Here is a line with a comment and sequence numbers, so you can see the layout:
000100* This line is a comment because column 7 holds a star.
000200 IDENTIFICATION DIVISION.
000300 DISPLAY "Area B starts in column 12".Modern COBOL also has free format, where columns mean nothing and *> starts a comment anywhere on a line. Write the same program that way and compile it with -free:
cd ~/hello-cobol-demo
cat > hello-free.cob <<'COB'
*> Free format: no column rules.
identification division.
program-id. hello-free.
procedure division.
display "Hello from free format".
stop run.
COB
cobc -x -free hello-free.cob
./hello-freeInstead of the -free option, you can switch format inside the file with the directive >>SOURCE FORMAT FREE. cobc reads the file in fixed format until it meets that line, so indent the directive by 7 spaces (column 8), the same as a division name. Every line after it is free format:
>>SOURCE FORMAT FREE
*> From here on, columns mean nothing.
identification division.Use free format for your own new programs. Expect fixed format in any code you are paid to maintain.
Check it worked
./hello-free prints Hello from free format. Then open hello.cob, delete the leading spaces from every line, and run cobc -x -fixed hello.cob: it now fails with errors that mention column 7. (Without -fixed, GnuCOBOL 3.2 notices that the first line starts in column 1, prints note: free format detected and compiles it as free format.) Put the spaces back (or rewrite the file with the first step's command) before you continue.
Step 4 · See the C code behind your program
cobc is a translator in front of a C compiler. Ask it to stop after the translation with -C, then look at what it wrote:
cd ~/hello-cobol-demo
cobc -C hello.cob
ls hello.c*
grep -n 'cob_display' hello.c
ldd hello | grep libcobcobc -Cwriteshello.cplus header files whose names start withhello.c. Without-C,cobcwrites the same files to a temporary folder, runsgccon them and deletes them.- The
grepfinds the line where yourDISPLAYbecame a call tocob_display, a function in GnuCOBOL's runtime library, libcob. lddshows that your program loadslibcobwhen it starts. Copy the program to another machine and that machine needs libcob too (on Ubuntu, thelibcob4t64package, whichgnucobol3installed for you).
Compiling via C is why GnuCOBOL runs on almost anything GCC supports, from a laptop to a Raspberry Pi, while mainframe compilers such as IBM Enterprise COBOL produce machine code directly.
Check it worked
ls lists hello.c and at least one hello.c...h header. The grep prints a line number and a call to cob_display. The ldd line names a file such as libcob.so.4.
Step 5 · Store a name in WORKING-STORAGE and ACCEPT it
Variables in COBOL live in the DATA DIVISION, and the WORKING-STORAGE SECTION holds the ones your program uses while it runs. Each variable has a PIC (picture) clause that fixes its type and size. Write a program that asks for a name:
cd ~/hello-cobol-demo
cat > greet.cob <<'COB'
IDENTIFICATION DIVISION.
PROGRAM-ID. GREET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(30).
PROCEDURE DIVISION.
DISPLAY "What is your name? " WITH NO ADVANCING.
ACCEPT WS-NAME.
DISPLAY "Hello, " FUNCTION TRIM(WS-NAME) "!".
STOP RUN.
COB
cobc -x greet.cobThe new lines:
DATA DIVISION.andWORKING-STORAGE SECTION.open the place where variables are declared. Both start in area A.01 WS-NAME PIC X(30).declares a top-level item (level01) namedWS-NAME.Xmeans any character and(30)means 30 of them, so this is always exactly 30 bytes long. Shorter text is padded with spaces; longer text is cut off. TheWS-prefix is a common habit that marks working-storage names; it is not required.DISPLAY ... WITH NO ADVANCINGprints without a newline, so the answer goes on the same line as the question.ACCEPT WS-NAME.reads one line from the keyboard into the variable.FUNCTION TRIM(WS-NAME)removes the padding spaces.DISPLAYcan take several items in a row and prints them one after another.
Run it and type your name when asked:
./greetThen try it with piped input, including a name with Yoruba tone marks:
cd ~/hello-cobol-demo
echo "Ada" | ./greet
printf 'Adébáyọ̀\n' | ./greetX(30) counts bytes, not letters. In UTF-8, Adébáyọ̀ is 7 letters but 13 bytes: é and á take 2 bytes each, ọ takes 3, and the grave accent on top of it is a separate 2-byte character. Names in Amharic, Arabic or Chinese take two to four bytes per letter, so size PIC X fields generously if your users write in those scripts.
Check it worked
The piped runs print What is your name? Hello, Ada! and What is your name? Hello, Adébáyọ̀!, each on one line. Now remove FUNCTION TRIM( and its closing ) from greet.cob, compile and run it again: the ! lands far to the right, after the padding spaces. That gap is the 30-byte field. Put TRIM back afterwards.
Step 6 · Add money with fixed-point decimals
Most languages store numbers like 0.10 as binary fractions, which cannot hold them exactly. In Python, 0.1 + 0.2 gives 0.30000000000000004. COBOL numbers are decimal: a picture of 9(5)V99 means five digits, an implied decimal point (V), then two digits. Write a program that adds two amounts:
cd ~/hello-cobol-demo
cat > budget.cob <<'COB'
IDENTIFICATION DIVISION.
PROGRAM-ID. BUDGET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TOTAL PIC 9(5)V99 VALUE ZERO.
01 WS-SHOWN PIC ZZ,ZZ9.99.
PROCEDURE DIVISION.
ADD 0.10 TO WS-TOTAL.
ADD 0.20 TO WS-TOTAL.
MOVE WS-TOTAL TO WS-SHOWN.
DISPLAY "Total: " WS-SHOWN.
STOP RUN.
COB
cobc -x budget.cob
./budgetPIC 9(5)V99 VALUE ZEROholds amounts from 0 to 99999.99 and starts at zero. TheVtakes no storage; it tells the compiler where the point goes.PIC ZZ,ZZ9.99is an edited picture, used only for display.Zprints a space instead of a leading zero, the comma and point are printed as written, and9always prints a digit.ADD 0.10 TO WS-TOTAL.reads as it is written.MOVE WS-TOTAL TO WS-SHOWN.copies the number into the display field, formatting it on the way.
This is why the language stayed in finance: the size and precision of every amount are written in the program, and the arithmetic matches what an accountant does on paper.
Check it worked
./budget prints Total: followed by spaces and 0.30, with no stray digits at the end. Change the first ADD to ADD 1234.50 TO WS-TOTAL., compile and run again, and you see Total: 1,234.70.
Step 7 · Recognise the errors beginners hit
Make the common mistakes on purpose, so you recognise them later.
Code starting in column 1. Write a file with no leading spaces and compile it as fixed format with -fixed:
cd ~/hello-cobol-demo
printf 'IDENTIFICATION DIVISION.\nPROGRAM-ID. BAD.\n' > bad.cob
cobc -x -fixed bad.cobcobc treats IDENTI as a sequence number and the F in column 7 as an indicator, and reports error: invalid indicator 'F' at column 7. Indent by 7 spaces, or compile with -free. Without -fixed, GnuCOBOL 3.2 looks at the first line, sees code in column 1, prints note: free format detected and reads the file as free format instead. Do not rely on that guess: other COBOL compilers do not make it, so keep fixed-format files indented.
Forgetting -x. Run cobc hello.cob without it. It succeeds, but you get hello.so, a module that other COBOL programs can call, not a program you can start with ./hello:
cd ~/hello-cobol-demo
cobc hello.cob
ls hello.soA missing closing quote. If you write DISPLAY "Hello. with no closing ", cobc reports an error about the unterminated text and gives the line number. Look at the line it names first.
Code past column 72. In fixed format, anything after column 72 is not part of the program. A long statement or text value that crosses that column loses its end, which shows up as a confusing error or wrong output. If a line looks right but behaves as if it was cut short, check its length; most editors show the column number in the status bar.
Check it worked
You have seen the column-7 error in your own terminal, and ls hello.so lists the module file that cobc built without -x.
Step 8 · Clean up
Delete the demo folder:
cd ~
rm -rf ~/hello-cobol-demoTo remove GnuCOBOL as well, run sudo apt remove gnucobol3. GCC stays installed, because other packages may use it.
Check it worked
ls ~/hello-cobol-demo reports No such file or directory.
Next steps
- Meet the other language built for numbers in the 1950s, still used for science: Hello World in Fortran.
- See strong typing taught as a first language: Hello World in Pascal.
- Try a language designed for systems that must not fail: Hello World in Ada.
Related tutorials
- Hello World in Zig: zig run, zig build and zig ccYou will install a signature-checked Zig 0.17.1 on Ubuntu 26.04, write and run Hello World with the current I/O API, build a small project with zig build that handles errors explicitly, and use zig cc to compile a C program for Linux and for Windows.
- Hello World in x86-64 assembly with NASM and Linux system callsYou will write an x86-64 assembly program that prints a line using only Linux system calls, assemble it with NASM, link it with ld, check its exit status, then loop it three times and read its machine code with objdump.
- Hello World in WebAssembly text format with wabt and WasmtimeYou will write a WebAssembly module by hand in the text format, assemble it with wat2wasm, print Hello World through WASI in Wasmtime 50, and call an exported add function from both Wasmtime and Node.js.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials