Module lessons (2/2)
Variables, PICTURE (PIC X/9/V/S) and Padding
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 a01record (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:
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:
X(Alphanumeric): Letters, digits and symbols (PIC X(12)= 12 bytes).9(Numeric): Digits0–9only (PIC 9(3)= 3 bytes).V(Implicit decimal point): Indicates where the decimal point falls in calculations without taking up a physical byte in RAM (PIC 9(4)V99takes up 6 bytes, not 7!).S(Sign): Retains the sign+/-along with the least significant digit (withoutS, 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"toPIC X(8)→"ALICE "). Longer strings are cut to the right.PIC 9(N): Align right and pad left with zeros (7toPIC 9(3)→"007"). Numbers with too many integer digits suffer most significant digit truncation (1234toPIC 9(3)→"234"!).
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:
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
Declare a level 01 alphanumeric variable called WS-USER-NAME of length 20 characters and set it with the initial value 'ALICE'.
Show hint
Use the format: 01 WS-USER-NAME PIC X(20) VALUE 'ALICE'.
Solution available after 3 attempts
Declares a 3-digit level 01 numeric variable called WS-USER-AGE with an initial value of 25.
Show hint
Use the syntax: 01 WS-USER-AGE PIC 9(3) VALUE 25.
Solution available after 3 attempts
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.
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
How many bytes does this field physically occupy in WORKING-STORAGE and what happens to the decimal point?
01 WS-INVOICE-TOTAL PIC S9(4)V99 VALUE -125.50.What is the exact content stored in WS-OUT after executing this MOVE?
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.