COBOL GnuCOBOL 3.2.0 ·
✓ verified by execution on 2026-08-16
In modern languages, you declare variables by specifying their type: integers, strings, floats. If an integer gets bigger, the language gives it more memory. If a string gets longer, the language automatically reallocates memory to fit it.
COBOL does not work this way. In COBOL, you do not declare a general type. Instead, you declare the exact physical shape and width of the variable in memory.
This is done using the PICTURE (or PIC) clause inside the WORKING-STORAGE SECTION of the DATA DIVISION.
The WORKING-STORAGE SECTION
Before you can use a variable, you must allocate memory for it. In COBOL, this happens in the DATA DIVISION.
cobol✓ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. WORKING-STORE. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC 9(5) VALUE 12345. PROCEDURE DIVISION. DISPLAY EMPLOYEE-ID. STOP RUN.
Output
12345
Your output
Let’s break down the declaration line: 01 EMPLOYEE-ID PIC 9(5) VALUE 12345.
01 (Level Number): This tells the compiler that EMPLOYEE-ID is a top-level, independent block of memory. It’s not just a bullet point; it defines the memory boundary!
EMPLOYEE-ID: The name of the variable.
PIC 9(5): The PICTURE clause. This dictates the variable’s physical shape.
VALUE 12345: The initial starting value.
The Shapes of Memory: 9 and X
The PIC clause uses specific characters to draw a “picture” of the memory:
9 stands for a single numeric digit.
X stands for a single alphanumeric character (a string).
You specify the length by repeating the character or putting the count in parentheses.
cobol✓ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. STRINGS-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-NAME PIC X(10) VALUE 'ALICE'. PROCEDURE DIVISION. DISPLAY EMPLOYEE-NAME. STOP RUN.
Output
ALICE
Your output
Notice the trailing spaces in the output! PIC X(10) creates a fixed block of exactly 10 characters. If you assign a 5-character string like 'ALICE', COBOL automatically pads the rest of the block with spaces on the right.
Similarly, if you put a small number into a large numeric field, COBOL pads it with zeros on the left:
cobol✓ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. ZERO-PAD. DATA DIVISION. WORKING-STORAGE SECTION. 01 SMALL-NUM PIC 9(5) VALUE 42. PROCEDURE DIVISION. DISPLAY SMALL-NUM. STOP RUN.
Output
00042
Your output
Visualizing Memory Slots
When you write PIC X(5), you are allocating a physical array of exactly 5 bytes.
The Danger of Truncation
Because memory is strictly physical, COBOL will never dynamically resize your variables.
If you try to shove a large value into a small PIC slot, something has to give. In COBOL, excess data is silently truncated.
Let’s see this in action. A PIC 9(3) is 3 digits wide. Its absolute maximum physical capacity is 999.
If we try to store the number 1000 inside a 3-digit box, what happens?
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. OVERFLOW-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 MAX-SCORE PIC 9(3) VALUE 1000. PROCEDURE DIVISION. DISPLAY MAX-SCORE. STOP RUN.
Output
000
You predicted
COBOL chops off the high-order (leftmost) digits that don’t fit!
The 1 in 1000 falls off the edge of memory, leaving only 000. This silent truncation can lead to catastrophic bugs in financial software if variables aren’t sized correctly from the start.
Similarly, if you place a long string into a short PIC X variable, COBOL will silently chop off the rightmost characters.
cobol✓ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. STR-TRUNC. DATA DIVISION. WORKING-STORAGE SECTION. 01 SHORT-NAME PIC X(4) VALUE 'ALEXANDER'. PROCEDURE DIVISION. DISPLAY SHORT-NAME. STOP RUN.
Output
ALEX
Your output
When you define a PIC clause, you aren’t just giving the compiler a hint; you are forging a hard physical limit in memory.
Check yourself
What happens if you try to store '1234' into a `PIC 9(3)` variable?
Reveal answer
The leftmost digit ('1') is silently truncated, resulting in '234'. — Because memory is fixed, assigning a value larger than the PIC clause's capacity results in silent high-order (leftmost) truncation.
Which statement correctly describes how `PIC X(5)` stores the string 'HI'?
Reveal answer
It pads the right side with spaces: 'HI ' — Alphanumeric fields (`PIC X`) pad with spaces on the right when the provided string is shorter than the defined width.
What does the `01` mean in `01 EMPLOYEE-ID PIC 9(5).`?
Reveal answer
It is a top-level independent data record. — Level number 01 declares a top-level data record in memory, marking the start of a distinct memory block.
Challenges
Challenge 1 +20 XP
This program tries to store the year 2026, but the output only shows '026'. Fix the PIC clause so it correctly holds the full four-digit year.
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 "2026\n"
Need a hint? (−25% XP)
Change the number in the parentheses after PIC 9 to match the number of digits in '2026'.
This program tries to display 'HELLO WORLD', but it gets cut off. Fix the PIC clause so the full string fits.
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 "HELLO WORLD\n"
Need a hint? (−25% XP)
Count the number of characters in 'HELLO WORLD' (including the space) and update the number in the PIC X parentheses.