HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to COBOLVariablesArithmeticConditions (IF)Loops (PERFORM)
REFERENCE
📖 Overview
🖥️ Structure & Output
📦 Variables
🔢 Arithmetic
🔀 Condition
🔁 Loop
COBOL›Variables

Variables

WORKING-STORAGE SECTION, types with PIC and assignment with MOVE.

Simple enough for a 5-year-old to understand

Variables with PIC

Variables are defined inside DATA DIVISION > WORKING-STORAGE SECTION. PIC X(n) is for text, PIC 9(n) is for numbers.

Example code
1
IDENTIFICATION DIVISION.
2
PROGRAM-ID. H.
3
DATA DIVISION.
Data division.
DefinitionData division.
4
WORKING-STORAGE SECTION.
Variables are here.
DefinitionVariables are here.
5
01 NAME PIC X(10) VALUE "Alex".
10-character text.
Variable10-character text.
6
PROCEDURE DIVISION.
7
DISPLAY NAME.
Alex
Output / InputAlex
8
STOP RUN.
DefinitionVariableOutput / Input
💡 Hint: PIC X(n) is n-character text, PIC 9(n) is n-digit number. VALUE gives an initial value.

About this topic

Variables in COBOL are declared in the working-storage section of the data division. Each declaration consists of a level number, a name, and a picture clause; the picture determines the character length and type of the data.

Level numbers establish a hierarchy: a lower-numbered item groups higher-numbered items beneath it. Thus, you can split an address record into street, city, and postal code fields, using both the whole and the parts by name.

Field lengths in COBOL are fixed. When a value longer than the width specified in the picture is assigned, the excess is silently truncated; short values are right-justified and padded with zeros for numeric fields, and left-justified and padded with spaces for text fields. This behavior is the language's most frequent source of bugs.

What you will learn

  • Working-storage declarations
  • Level numbers and groups
  • The picture clause
  • Initializing values
  • Truncation and padding behavior

Who is this for?

  • Learners of COBOL data structures
  • Those encountering field length errors
  • Record layout analysts

When to use?

When working with fixed-width file and record layouts, group items are the clearest way to document the data structure directly in code.

For monetary amounts, numeric fields with decimal places specified in the picture clause are used; this completely avoids the rounding issues of floating-point arithmetic.

Common mistakes

Wrong

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AD PIC X(5).
PROCEDURE DIVISION.
    MOVE 'Istanbul' TO WS-AD.
    DISPLAY WS-AD.
    STOP RUN.

Since the field is five characters wide, only the first five characters of the value are stored, and the rest are silently discarded. The compiler sometimes issues a warning, but the program runs and the data loss goes unnoticed. Field widths must be chosen based on actual data.

Correct

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AD PIC X(20).
PROCEDURE DIVISION.
    MOVE 'Istanbul' TO WS-AD.
    DISPLAY WS-AD.
    STOP RUN.

Wrong

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SAYI PIC 9(3).
PROCEDURE DIVISION.
    MOVE 1250 TO WS-SAYI.
    DISPLAY WS-SAYI.
    STOP RUN.

Because numeric fields are right-justified, extra digits are truncated from the left; two hundred fifty appears on the screen. This is a situation that causes the amount in financial calculations to turn out completely wrong without generating any error messages. Field width must be chosen based on the maximum possible value.

Correct

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SAYI PIC 9(5).
PROCEDURE DIVISION.
    MOVE 1250 TO WS-SAYI.
    DISPLAY WS-SAYI.
    STOP RUN.

Wrong

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TOPLAM PIC 9(5).
PROCEDURE DIVISION.
    ADD 100 TO WS-TOPLAM.
    DISPLAY WS-TOPLAM.
    STOP RUN.

Since the field is not initialized, its initial content is undefined; in some environments, it contains a space character, causing numeric operations to produce unexpected results or runtime errors. Accumulator fields must be assigned zero using the value clause.

Correct

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TOPLAM PIC 9(5) VALUE ZERO.
PROCEDURE DIVISION.
    ADD 100 TO WS-TOPLAM.
    DISPLAY WS-TOPLAM.
    STOP RUN.

Frequently asked questions

What do the letters in the picture clause mean?

Nine means a numeric digit, X means any character, and A means a letter only. The number in parentheses is the repetition count. S adds a sign, and V adds an invisible decimal separator; V takes up no space in the stored data, it only tells where the digits are divided.

How does a group item behave?

A group is the contiguous combination of the fields beneath it and is always treated as a character type. Assigning a number to a group does not perform digit alignment, it copies characters; therefore, calculations must always be done with the lowest-level numeric fields.

What is packed decimal used for?

It stores numbers so that two digits fit into a single byte; it saves space and speeds up arithmetic on mainframe hardware. It is the standard choice for batch jobs with large data volumes, but you must pay attention to type compatibility in file layout definitions.

Summary

Variables are declared in the working-storage section.

Level numbers establish a hierarchy.

The picture clause determines type and width.

Overflow values are silently truncated.

Accumulators must be initialized.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code