Skip to main content
Module 1 · Lesson 2 of 22/16 in the course~15 min
Module lessons (2/2)

Variables, PICTURE (PIC X/9/V/S) and Padding

Build Stage

Static memory & PICTURE clause

In COBOL each variable has a fixed size known to the compiler: understanding bytes in RAM eliminates silent accounting bugs.

In COBOL there are no dynamic types or garbage-collected heaps for ordinary variables: all local memory is statically allocated in DATA DIVISION inside WORKING-STORAGE SECTION, specifying exactly how many characters or digits each field will occupy.

1. Level Numbers and Clause PIC

Each declared data begins with a 2-digit level number:

  • 01: Main record or elementary independent variable (located in Area A, col 8).
  • 05, 10, 15: Subordinate fields within a 01 record (located in Area B, col 12).
  • 77: Level history for non-decomposable elementary scalar variables (modern equivalent: 01).

The keyword PICTURE (almost always abbreviated to PIC) defines format and length in bytes:

COBOL
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-PROJECT-NAME PIC X(12)    VALUE "ELEARNER".
       01 WS-RETRY-COUNT  PIC 9(3)     VALUE 7.
       01 WS-UNIT-PRICE   PIC 9(4)V99  VALUE 149.90.
       01 WS-DELTA        PIC S9(4)V99 VALUE -25.50.

The four key characters of PIC:

  1. X (Alphanumeric): Letters, digits and symbols (PIC X(12) = 12 bytes).
  2. 9 (Numeric): Digits 0–9 only (PIC 9(3) = 3 bytes).
  3. V (Implicit decimal point): Indicates where the decimal point falls in calculations without taking up a physical byte in RAM (PIC 9(4)V99 takes up 6 bytes, not 7!).
  4. S (Sign): Retains the sign +/- along with the least significant digit (without S, a negative value silently becomes positive!).

2. Opposite Padding (X vs 9) and Silent Truncation

When you assign with VALUE or MOVE a data shorter or longer than PIC, COBOL does not throw exceptions: it truncates or pads according to opposite rules for type:

  • PIC X(N): Align left and pad right with spaces ("ALICE" to PIC X(8) → "ALICE "). Longer strings are cut to the right.
  • PIC 9(N): Align right and pad left with zeros (7 to PIC 9(3) → "007"). Numbers with too many integer digits suffer most significant digit truncation (1234 to PIC 9(3) → "234"!).
COBOL
       01 WS-CODE      PIC X(6) VALUE "AB".      *> RAM: "AB    "
       01 WS-ITEM-NO   PIC 9(4) VALUE 42.        *> RAM: "0042"
       01 WS-SHORT-ID  PIC 9(2) VALUE 125.       *> Troncato a 25!

3. Figurative Constants (SPACES, ZEROS) and Edited Masks (Z)

To clear or clear fields without depending on the specific length, COBOL offers figurative constants:

COBOL
       01 WS-LABEL     PIC X(10) VALUE SPACES.
       01 WS-ACCUM     PIC 9(6)V99 VALUE ZEROS.
       01 WS-PRINTED   PIC ZZ,ZZ9.99 VALUE ZERO.

       PROCEDURE DIVISION.
           MOVE 1250.50 TO WS-ACCUM.
           MOVE WS-ACCUM TO WS-PRINTED.
           DISPLAY "Formatted: " WS-PRINTED.
           STOP RUN.

Try it yourself

Exercise#cobol.m1.l2.e1
Attempts: 0Loading…

Declare a level 01 alphanumeric variable called WS-USER-NAME of length 20 characters and set it with the initial value 'ALICE'.

Loading editor…
Show hint

Use the format: 01 WS-USER-NAME PIC X(20) VALUE 'ALICE'.

Solution available after 3 attempts

Exercise#cobol.m1.l2.e2
Attempts: 0Loading…

Declares a 3-digit level 01 numeric variable called WS-USER-AGE with an initial value of 25.

Loading editor…
Show hint

Use the syntax: 01 WS-USER-AGE PIC 9(3) VALUE 25.

Solution available after 3 attempts

Exercise#cobol.m1.l2.e3
Attempts: 0Loading…

Declare a numeric variable with sign and implicit decimals called WS-NET-BALANCE of type PIC S9(5)V99 with initial value -150.50, then print it with DISPLAY in the PROCEDURE DIVISION.

Loading editor…
Show hint

In WORKING-STORAGE declare 01 WS-NET-BALANCE PIC S9(5)V99 VALUE -150.50. and in PROCEDURE DIVISION add DISPLAY 'BAL: ' WS-NET-BALANCE.

Solution available after 3 attempts

COBOL Quiz#cobol.m1.l2.e4
Ready

How many bytes does this field physically occupy in WORKING-STORAGE and what happens to the decimal point?

COBOL
       01 WS-INVOICE-TOTAL PIC S9(4)V99 VALUE -125.50.
Answer options
COBOL Quiz#cobol.m1.l2.e5
Ready

What is the exact content stored in WS-OUT after executing this MOVE?

COBOL
       01 WS-CODE PIC X(6) VALUE SPACES.
       01 WS-QTY  PIC 9(4) VALUE ZEROS.
       PROCEDURE DIVISION.
           MOVE "OK" TO WS-CODE.
           MOVE 12   TO WS-QTY.
Answer options