PICTURE: X, 9, A and mixed fields

A field is declared with a picture, and the picture is the field: it says how many bytes the field takes and what each byte may hold. The clause looks like a mask because it is one, one character per byte, and the parentheses are the only shorthand in it.

A picture is a template

Each character of the picture is one position of the field, and a number in parentheses repeats the character before it: PIC X(5) is PIC XXXXX, five positions, and PIC 9(3) is PIC 999, three digits. The field takes exactly as many bytes as the picture has positions, in the order they are written.

IDENTIFICATION DIVISION. PROGRAM-ID. PIC1. DATA DIVISION. WORKING-STORAGE SECTION. 01 TXT-FIELD PIC X(5) VALUE "ab cd". 01 NUM-FIELD PIC 9(3) VALUE 42. 01 ALPHA-FIELD PIC A(4) VALUE "AB". 01 MONEY-FIELD PIC 9(3)V99 VALUE 12.5. PROCEDURE DIVISION. DISPLAY "X(5) [" TXT-FIELD "] " LENGTH OF TXT-FIELD. DISPLAY "9(3) [" NUM-FIELD "] " LENGTH OF NUM-FIELD. DISPLAY "A(4) [" ALPHA-FIELD "] " LENGTH OF ALPHA-FIELD. DISPLAY "9(3)V99 [" MONEY-FIELD "] " LENGTH OF MONEY-FIELD. STOP RUN.

cobc -x -free -Wall -o pic1 pic.cob
./pic1

X(5) [ab cd] 5
9(3) [042] 3
A(4) [AB ] 4
9(3)V99 [012.50] 5

LENGTH OF is a compile-time function: it asks the picture, not the program, and it costs nothing at run time. The fourth line is the one to look at twice: five bytes, and a decimal point that was never stored.

X, 9 and A

Three picture characters cover most fields:

  1. X is one character of any kind: letters, digits, spaces, punctuation. A field of X is a string of bytes and the compiler checks nothing about it.
  2. 9 is one digit, 0 to 9. A field of 9 is a number written in characters, so PIC 9(3) holds 042, not 42.
  3. A is one alphabetic character and one space. The letter is a claim about the field, not a cage: the compiler warns when a value cannot satisfy it and stores the value anyway.

IDENTIFICATION DIVISION. PROGRAM-ID. AC. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-FIELD PIC A(2) VALUE "12". PROCEDURE DIVISION. DISPLAY "[" A-FIELD "]". STOP RUN.

cobc -x -free -Wall -o ac ac.cob
./ac

ac.cob:6: warning: value does not fit the picture string [-Wothers]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 A-FIELD PIC A(2) VALUE "12".
7 |
8 | PROCEDURE DIVISION.
[12]

The build succeeds, the value is stored, and the warning is the only sign that the declaration and the data disagree. A picture is a description that a reader and a compiler can check; it is not a validator that stops a program at run time.

PIC 9(3)V99 and the point

V is the implied decimal point: it marks the position of the point without taking a byte. A field of PIC 9(3)V99 has five digit positions and no sixth character, and the value 12.5 fills it as 01250. GnuCOBOL inserts the point when it displays the field, which is convenient and is not what every compiler does.

A point written as a character is a different picture. In PIC 9(3).99 the period is part of the field, so the field takes six bytes and the point is stored with the digits. The two pictures print the same thing and are not the same size:

IDENTIFICATION DIVISION. PROGRAM-ID. EDITED. DATA DIVISION. WORKING-STORAGE SECTION. 01 PLAIN-FIELD PIC 9(3)V99 VALUE 12.5. 01 EDITED-FIELD PIC 9(3).99 VALUE 12.5. PROCEDURE DIVISION. DISPLAY "[" PLAIN-FIELD "] " LENGTH OF PLAIN-FIELD. DISPLAY "[" EDITED-FIELD "] " LENGTH OF EDITED-FIELD. STOP RUN.

cobc -x -free -Wall -o edited edited.cob
./edited

[012.50] 5
[012.50] 6

The rule that follows is the one to memorize: V takes no byte, every other character of a picture takes one. A file record full of edited fields is a byte per field wider than the same record with implied points, which matters when the record has a fixed layout.

Mixed pictures

The three characters may be combined in one picture, and each keeps its own positions. A product code of two letters and four digits, and a room number of two letters and two digits:

IDENTIFICATION DIVISION. PROGRAM-ID. MIXED. DATA DIVISION. WORKING-STORAGE SECTION. 01 CODE-FIELD PIC X(2)9(4) VALUE "AB1234". 01 ROOM-FIELD PIC A(2)9(2) VALUE "RM01". PROCEDURE DIVISION. DISPLAY "CODE [" CODE-FIELD "] " LENGTH OF CODE-FIELD. DISPLAY "ROOM [" ROOM-FIELD "] " LENGTH OF ROOM-FIELD. STOP RUN.

cobc -x -free -Wall -o mixed mixed.cob
./mixed

CODE [AB1234] 6
ROOM [RM01] 4

The mixed picture is not a type: the field is six characters, and a MOVE into it copies characters like any other alphanumeric field. What the pictures buy is a check at build time for literals the compiler can see, and a description the next reader can believe.

What a picture claims

The last example is the one that surprises people: a field of PIC 9(3) can hold letters, because at run time it is three characters and the move copies characters. The compiler warns when it can see the mistake in the source:

IDENTIFICATION DIVISION. PROGRAM-ID. BAD. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM-FIELD PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE "AB" TO NUM-FIELD. DISPLAY "[" NUM-FIELD "]". STOP RUN.

cobc -x -free -Wall -o bad bad.cob
./bad

bad.cob:9: warning: numeric value is expected [-Wtyping]
7 |
8 | PROCEDURE DIVISION.
9 > MOVE "AB" TO NUM-FIELD.
10 | DISPLAY "[" NUM-FIELD "]".
11 | STOP RUN.
bad.cob:6: note: 'NUM-FIELD' defined here as PIC 9(3) [-Wtyping]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 NUM-FIELD PIC 9(3) VALUE 0.
7 |
8 | PROCEDURE DIVISION.
[0AB]

[0AB] is not a number and the program still built. A later ADD on that field would compute with something that is not a number, which is the reason the course builds everything with -Wall and reads the warnings. The lesson on USAGE DISPLAY shows the byte that makes this possible.

Exercises

  1. Declare a field of PIC X(10) and a field of PIC 9(10), set both to 1234567890 and print each with its length. Which one keeps all ten characters, and which one loses something on the way in?
  2. Change PIC 9(3)V99 to PIC 9(3).99 in the money field of the first program and build again. What does the output look like, and what does the length become?
  3. Declare PIC A(3), move a three-letter name into it and then a three-character code with a digit. Which of the two builds prints a warning, and what does the program print in each case?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does the number in parentheses of a picture do?

    Answer

    It repeats the picture character before it

    PIC X(5) is five positions of X and PIC 9(3) three of 9; there is no other shorthand in a picture.

  2. Which picture stores a decimal point as a byte of its own?

    Answer

    PIC 9(3).99

    A period in the picture is a position that holds the character; V is the implied point and takes no byte.

  3. How many bytes does a field of PIC X(7) take?

    Answer

    7

    The picture has seven positions and a field is as wide as its picture, whatever value is moved into it.

  4. What does PIC A claim about a position?

    Answer

    That it holds an alphabetic character or a space

    A is the alphabetic picture; the compiler warns when a literal cannot satisfy it and stores the value anyway.

  5. What is printed by DISPLAY LENGTH OF F for a field declared 01 F PIC 9(3)V99.?

    Answer

    5

    Three digits before the point and two after it are five positions, and V itself is not stored.

  6. What is printed by DISPLAY LENGTH OF F for a field declared 01 F PIC 9(3).99.?

    Answer

    6

    The period is one of the positions of the picture, so the field holds five digits and the point.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-FIELD PIC A(2) VALUE "12". PROCEDURE DIVISION. DISPLAY "[" A-FIELD "]". STOP RUN.

    Answer

    [12]

    The compiler warns that the value does not fit the picture and stores it anyway, so the two characters are printed.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM-FIELD PIC 9(3) VALUE 0. PROCEDURE DIVISION. MOVE "AB" TO NUM-FIELD. DISPLAY "[" NUM-FIELD "]". STOP RUN.

    Answer

    [0AB]

    The move copies two characters into the right of the field and leaves a zero in front, and the typing warning does not stop the build.

  9. A PIC 9(3) field holds AB after a move from an alphanumeric field. Why?

    Answer

    The picture describes the field and does not check it at run time

    A field of 9 is three characters at run time; the compiler warns where it can see the mismatch, and the move still copies characters.

  10. What does GnuCOBOL print for a field of PIC 9(3)V99 that holds 12.5?

    Answer

    012.50

    The field holds five digits and GnuCOBOL inserts the implied point when it displays them, so the point appears without being stored.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev