Menu
CoddyTech

Too Long, Too Short

Part of the First Programs section of Coddy's COBOL Programming journey. Lesson 9 of 19.

A field never changes size. Text longer than a PIC X field keeps its left part and loses the rest:

01 CITY PIC X(5).
    MOVE "Barcelona" TO CITY. *> Barce

Shorter text is filled out with spaces on the right, as you saw with VALUE.

Numbers line up on the right, so a number with too many digits loses digits on the left:

01 QTY PIC 9(3).
    MOVE 1234 TO QTY. *> 234

COBOL gives no warning when it cuts. Pick pictures big enough for the largest value a field will ever hold.

challenge icon

Challenge

Easy

The program prints 765 because NUM is too small. Change its picture so the program prints 98765.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. CUT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUM PIC 9(3).
       PROCEDURE DIVISION.
           MOVE 98765 TO NUM.
           DISPLAY NUM.
           STOP RUN.

All lessons in First Programs