Variables and Types in COBOL
Learn how COBOL defines variables with PIC clauses, level numbers, and WORKING-STORAGE — the unique data model behind the language that runs the world
COBOL’s approach to variables is unlike any modern language. Rather than declaring int x = 5 or name: str, COBOL uses PIC clauses (PICTURE clauses) that describe the physical format of each data item — specifying exactly how many digits, characters, or decimal places a variable can hold. This reflects COBOL’s business-data origins: a variable isn’t just a type, it’s a layout specification.
All variables in a COBOL program are defined in the DATA DIVISION, specifically in the WORKING-STORAGE SECTION. There are no variable declarations scattered through procedure code — every variable is declared upfront, organized in a hierarchical structure using level numbers. This keeps data definitions centralized and makes programs easier to audit, a critical property for financial systems.
COBOL’s type system is statically typed and strong. The compiler knows the format of every variable at compile time, and conversions between numeric and text types must be done explicitly. This strictness has proven valuable over decades of business computing — implicit type coercions that silently corrupt financial data are not an option.
In this tutorial you’ll learn how to declare numeric variables, alphanumeric strings, and structured records, and how COBOL’s level-number hierarchy creates data structures without a separate struct keyword.
PIC Clauses and Level Numbers
Every COBOL variable declaration has this form:
level-number data-name PIC picture-string [VALUE initial-value].
The level number determines the variable’s place in a hierarchy:
01— Top-level group or elementary item02–49— Subordinate items nested inside a group77— Standalone elementary item (no group membership)88— Condition name (a named boolean test on a parent item)
The PIC string describes the data format using symbols:
| Symbol | Meaning |
|---|---|
X | Any alphanumeric character |
9 | Numeric digit (0–9) |
A | Alphabetic character |
V | Implied decimal point (no stored character) |
S | Sign (+ or −) |
Repetitions are written as X(10) (ten characters) or 9(5) (five digits).
Example 1: Basic Variable Declarations
Create a file named variable.cob:
| |
Running with Docker
| |
Expected Output
Name: Alice
Age: 030
Salary: 005200000
Temperature: -015
Active: Y
Note on COBOL numeric display:
WS-SALARYis stored as9(7)V99— seven integer digits plus two implied decimal places, giving nine raw digit positions total. When displayed withDISPLAY, GnuCOBOL prints all nine digits without the decimal point (sinceVis an implied point, not a stored character), so 52000.00 appears as005200000. Use a separate edited (formatted) field withPIC ZZZ,ZZZ.99to display with a real decimal point. Similarly,WS-AGEis three digits wide so it displays as030, andWS-TEMPERATURE PIC S9(3)stores three digit positions plus a sign, displaying as-015.
Example 2: Group Items and Nested Level Numbers
COBOL builds record structures using level-number hierarchies. A level-01 item with nested levels becomes a group item — COBOL’s equivalent of a struct.
Create a file named variables-group.cob:
| |
Running with Docker
| |
Expected Output
Date: 2026-03-11
Employee: 100042 Grace Hopper
Department: Engineer
Group items as strings: A group item like
WS-DATEcan also be referenced as a whole — it behaves as an alphanumeric string formed by concatenating all subordinate fields. This is how COBOL handles structured I/O: you can read or write the entire record in one operation.
Example 3: Constants, Condition Names, and Type Conversions
COBOL provides two mechanisms for constants and a unique feature — condition names (88 level) — that create named boolean tests on a variable’s value.
Create a file named variables-const.cob:
| |
Running with Docker
| |
Expected Output
Company: CodeArchaeology
Pi: 3141593
Max retries: 05
42 * Pi = 000013194
Order has shipped.
Order is not yet complete.
Condition names are COBOL’s boolean type. Rather than using a separate boolean type, COBOL lets you attach named conditions to any variable’s possible values.
IF STATUS-SHIPPEDreads naturally and is far clearer thanIF WS-ORDER-STATUS = "S"scattered through hundreds of paragraphs.
Key Concepts
- PIC clauses define storage format —
9(5)means five decimal digits,X(20)means twenty alphanumeric characters. The format determines what values are valid. Vis an implied decimal point —9(5)V99stores seven digits but treats the last two as cents. No decimal character is stored; arithmetic uses the implied position.Sprefix enables signed numbers — WithoutS, a numeric variable cannot represent negative values.- Level numbers create hierarchy —
01is the top level; items nested inside with higher numbers (05, 10, etc.) form group records equivalent to structs in other languages. 88condition names are COBOL booleans — Attach meaningful names to valid states of a variable; test withIF condition-namefor readable, maintainable code.ZEROS,SPACES,HIGH-VALUESare figurative constants —VALUE ZEROSinitializes numerics to zero;VALUE SPACESinitializes alphanumerics to spaces.- All variables live in DATA DIVISION — Unlike C, Java, or Python, COBOL does not allow variable declarations inside procedure code. Every data item is declared upfront.
- MOVE handles type coercion — Moving an alphanumeric
"42"into a numeric field converts the string to a number. COBOL does this implicitly on MOVE, but arithmetic directly on alphanumeric fields is an error.
Running Today
All examples can be run using Docker:
docker pull esolang/cobol:latest
Comments
Loading comments...
Leave a Comment