COBOL GnuCOBOL 3.2.0 ·
✓ verified by execution on 2026-08-16
When you declare a variable in COBOL’s WORKING-STORAGE SECTION, you ask the computer for a specific chunk of memory. But what’s actually inside that memory when the program starts?
If you don’t explicitly tell COBOL what to put there, the answer is garbage.
cobol✓ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. SAFE-INIT. DATA DIVISION. WORKING-STORAGE SECTION. 01 CLEAN-NUMBER PIC 9(3) VALUE 42. PROCEDURE DIVISION. DISPLAY CLEAN-NUMBER. STOP RUN.
Output
042
Your output
The Danger of Raw Memory
In modern languages like Python or JavaScript, a new variable is automatically initialized to a clean state like undefined, null, or 0. A modern programmer intuitively expects “uninitialized” to mean “empty”.
That intuition fails in COBOL because the runtime does not garbage-collect or zero-out RAM when allocating variables. “Empty” literally means whatever random bits of data were left behind in that physical memory address by the last program that used it.
Why did the language designers build it this way? In the 1960s, wasting CPU cycles automatically zeroing out memory was considered too expensive, especially if the program was just going to immediately overwrite that memory by reading a record from a magnetic tape anyway. Performance was paramount, so the OS just handed you the raw bytes.
If you attempt to perform math on this garbage data, your program will crash or produce silent, incorrect calculations.
The Boundary: FILE and LINKAGE SECTION
The danger of raw, uninitialized memory only applies to the WORKING-STORAGE SECTION (which actually allocates new memory). In the FILE SECTION (reading from disk) and the LINKAGE SECTION (receiving parameters from another program), COBOL actually ignores the VALUE clause completely. Those sections don’t allocate new memory; they just map your variable names onto existing buffers that already contain data.
Attempting to perform math on garbage data will cause your program to crash or produce silent, incorrect calculations.
The VALUE Clause
To safely initialize a variable when the program loads, use the VALUE clause.
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. VAL-NUM. DATA DIVISION. WORKING-STORAGE SECTION. 01 SCORE PIC 9(3) VALUE 100. 01 GREETING PIC X(5) VALUE 'HELLO'. PROCEDURE DIVISION. DISPLAY "Score is: " SCORE. DISPLAY "Greeting is: " GREETING. STOP RUN.
Output
Score is: 100
Greeting is: HELLO
You predicted
Figurative Constants
COBOL has built-in keywords called figurative constants that represent common data patterns. They automatically scale to match the size of your PIC clause, so you don’t have to type out long strings of identical characters.
ZEROS (or ZERO, ZEROES): Fills numeric fields with zeros.
SPACES (or SPACE): Fills alphanumeric fields with blank spaces.
The VALUE clause only works exactly once: when the program is first loaded into memory. If you want to reset a variable back to a blank state during the execution of your program, you must use the INITIALIZE verb in the PROCEDURE DIVISION.
INITIALIZE wipes a data item clean, safely resetting it to its default state: spaces for alphanumeric variables, and zeros for numeric variables.
If you INITIALIZE a group item (a record containing multiple sub-variables), COBOL is smart enough to reset the numbers to 0 and the strings to spaces automatically!
Check yourself
What is the initial value of a WORKING-STORAGE variable if you do NOT include a VALUE clause?
Reveal answer
It is undefined and contains leftover garbage data from memory. — Standard COBOL leaves uninitialized memory exactly as it found it, meaning it contains whatever random bytes were previously in that memory location.
Which statement is true about the difference between VALUE and INITIALIZE?
Reveal answer
VALUE sets the initial state when the program loads, INITIALIZE resets the state during execution. — VALUE is evaluated at compile time to set the starting memory. INITIALIZE is an executable verb that wipes memory back to spaces or zeros at runtime.
What does the figurative constant SPACES represent?
Reveal answer
A string of blank space characters. — SPACES (or SPACE) represents one or more blank characters, automatically scaling to fill the PIC clause width.
Challenges
Challenge 1 +20 XP
This program expects the counter to start at zero, but it was left uninitialized. Fix the code by adding the correct VALUE clause.
cobol✓ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real — it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 — expects "000\n"
Need a hint? (−25% XP)
Add 'VALUE ZEROS' or 'VALUE 0' to the PIC 9(3) declaration line.
The game has restarted! Use the INITIALIZE command to wipe the current score back to zero before displaying it.
cobol✓ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real — it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 — expects "0000\n"
Need a hint? (−25% XP)
Add 'INITIALIZE CURRENT-SCORE.' just before the DISPLAY statement.