Level 78 and figurative constants

Two kinds of constant sit in the data division. A level 78 gives a name to a number or a literal, so the size of a buffer is written once and used in five places. The figurative constants are the opposite: words like SPACE and HIGH-VALUE that stand for a character the program would rather not spell.

A name for a number

The banner below has a width in three different places: the picture of the line, the number of items in the table and the number of passes of the loop. One 78 names it, and the three places read the name.

IDENTIFICATION DIVISION. PROGRAM-ID. CONSTS. DATA DIVISION. WORKING-STORAGE SECTION. 78 LINE-WIDTH VALUE 12. 78 MAX-ITEMS VALUE 3. 01 LINE-BUF PIC X(LINE-WIDTH) VALUE ALL "-". 01 COUNTER-F PIC 9(2) VALUE 0. 01 ITEM-TBL. 05 ITEM-F PIC X(2) OCCURS MAX-ITEMS TIMES. PROCEDURE DIVISION. DISPLAY "[" LINE-BUF "]". DISPLAY "max items " MAX-ITEMS. PERFORM MAX-ITEMS TIMES ADD 1 TO COUNTER-F END-PERFORM. DISPLAY "count " COUNTER-F. MOVE "AB" TO ITEM-F(2). DISPLAY "second [" ITEM-F(2) "]". STOP RUN.

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

[------------]
max items 3
count 03
second [AB]

A 78 takes no storage: it is not a field with a value, it is a name the compiler replaces with the literal before it reads the rest. That is why it works in a picture, in an OCCURS clause and in a PERFORM count, three places where a data name would be refused.

Where a 78 may stand

Anywhere a literal may stand, because that is what it becomes. It may be displayed, compared, added and moved from:

IDENTIFICATION DIVISION. PROGRAM-ID. RULESET. DATA DIVISION. WORKING-STORAGE SECTION. 78 MAX-ITEMS VALUE 3. 01 COUNTER-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. ADD MAX-ITEMS TO COUNTER-F. DISPLAY COUNTER-F. STOP RUN.

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

03

It may not be written to. A 78 has no storage and no picture, so a statement that expects a receiving field finds a number instead, and the compiler says so in the terms of the substitution:

IDENTIFICATION DIVISION. PROGRAM-ID. RULEWRITE. DATA DIVISION. WORKING-STORAGE SECTION. 78 MAX-ITEMS VALUE 3. 01 COUNTER-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. MOVE 5 TO MAX-ITEMS. DISPLAY MAX-ITEMS. STOP RUN.

cobc -x -free -Wall -o rulewrite rulewrite.cob

rulewrite.cob:10: error: invalid MOVE target: 3
8 |
9 | PROCEDURE DIVISION.
10 > MOVE 5 TO MAX-ITEMS.
11 | DISPLAY MAX-ITEMS.
12 | STOP RUN.

The message says target: 3 and not target: MAX-ITEMS, which is the clearest demonstration of what a 78 is: by the time the statement is checked, the name is gone.

What a 78 may not have

The entry needs a value and nothing else. A picture would describe storage, and there is no storage:

IDENTIFICATION DIVISION. PROGRAM-ID. RULEPIC. DATA DIVISION. WORKING-STORAGE SECTION. 78 BAD-CONST VALUE 10 PIC 9(2). PROCEDURE DIVISION. DISPLAY BAD-CONST. STOP RUN.

cobc -x -free -Wall -o rulepic rulepic.cob

rulepic.cob:6: error: syntax error, unexpected PICTURE
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 78 BAD-CONST VALUE 10 PIC 9(2).
7 |
8 | PROCEDURE DIVISION.

A 78 is a literal with a name, so its value is one literal. A table of sizes, a set of codes that have to be looked up, or a value that changes during the run is a level 01 or 77 field with a VALUE clause, and the 78 is for the numbers that are decided before the program exists.

The figurative constants

Six words stand for characters that are awkward to write: a space, a zero, the highest and the lowest byte, a quote, and the repetition of a literal. Each has a real value, and the run below prints it:

IDENTIFICATION DIVISION. PROGRAM-ID. FIGS. DATA DIVISION. WORKING-STORAGE SECTION. 01 SPACES-F PIC X(4) VALUE SPACES. 01 ZEROS-F PIC 9(4) VALUE ZERO. 01 ZTEXT-F PIC X(4) VALUE ZERO. 01 HIGH-F PIC X(2) VALUE HIGH-VALUE. 01 LOW-F PIC X(2) VALUE LOW-VALUE. 01 QUOTED PIC X(5) VALUE QUOTE. 01 STARS PIC X(4) VALUE ALL "*". PROCEDURE DIVISION. DISPLAY "spaces [" SPACES-F "]". DISPLAY "zeros [" ZEROS-F "]". DISPLAY "ztext [" ZTEXT-F "]". DISPLAY "high " FUNCTION HEX-OF(HIGH-F). DISPLAY "low " FUNCTION HEX-OF(LOW-F). DISPLAY "quoted [" QUOTED "]". DISPLAY "stars [" STARS "]". STOP RUN.

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

spaces [ ]
zeros [0000]
ztext [0000]
high FFFF
low 0000
quoted ["""""]
stars [****]

HIGH-VALUE is a byte of all ones, FF in hex, and sorts above every printable character; LOW-VALUE is the zero byte and sorts below them. Neither prints as anything, which is why the hex is the useful view: a field left at LOW-VALUE is not a field of spaces, and a report that prints it prints nothing at all.

How they fill a field

A figurative constant is one character repeated to the length of the receiving field, and that is true of QUOTE too: VALUE QUOTE in a five-byte field is five quote marks, not one quote and four spaces. The ["""""] of the run above is the output, and it is easy to read as a mistake until the rule is known.

ZERO filling a PIC X field writes the character 0 four times, so the text "0000" in the run is the digit and not an empty value. The difference matters when the field is compared with SPACE: a zero-filled text field fails that comparison, and the program that treats "blank" as "missing" misses it.

ALL is the only one that takes an argument, and it repeats a whole literal rather than a character: ALL "AB" in six bytes is ABABAB, as the lesson on VALUE shows. A literal that fills the field exactly is the same thing, and ALL is how a program says that the repetition is meant.

Exercises

  1. Change LINE-WIDTH to 20 and build. Which lines of the output change, and which do not?
  2. Declare 78 COUNT-LIMIT VALUE 100. and use it as the upper bound of a PERFORM VARYING. What does the loop do, and what would a 01 field with the same value have done?
  3. Print the hex of a PIC X(3) VALUE LOW-VALUE and of a PIC X(3) VALUE SPACE. Which one compares equal to SPACE?
  4. Give a field VALUE ALL "0" and compare it with ZERO and with "0". Read the two results and explain them from the picture.

Questions

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

  1. What is a level 78 entry?

    Answer

    A name the compiler replaces with a literal, with no storage

    The 78 is substituted before the statement is checked, so it may stand where a literal stands and may not be written to.

  2. Which clause may a level 78 carry?

    Answer

    VALUE, and nothing that describes storage

    A picture or a usage would describe bytes, and a 78 has none; the compiler refuses a picture with a syntax error.

  3. What does VALUE QUOTE put into a PIC X(5) field?

    Answer

    Five quote characters

    A figurative constant is one character repeated to the width of the field, and QUOTE is no exception.

  4. What byte value does HIGH-VALUE stand for?

    Answer

    FF, the highest byte

    HIGH-VALUE is all ones and sorts above every printable character; LOW-VALUE is all zeros and sorts below them.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 78 WIDTH VALUE 6. 01 LINE-BUF PIC X(WIDTH) VALUE ALL "*". PROCEDURE DIVISION. DISPLAY "[" LINE-BUF "]". STOP RUN.

    Answer

    [******]

    The 78 is replaced by 6 before the picture is read, so the field is six bytes and ALL fills all six.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 78 MAX-ITEMS VALUE 3. 01 COUNTER-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. ADD MAX-ITEMS TO COUNTER-F. DISPLAY COUNTER-F. STOP RUN.

    Answer

    03

    The 78 is the literal 3 in an ADD, and the receiving field is two digits, so the sum prints as 03.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 ZTEXT-F PIC X(3) VALUE ZERO. PROCEDURE DIVISION. DISPLAY "[" ZTEXT-F "]". STOP RUN.

    Answer

    [000]

    ZERO in an alphanumeric field is the character 0 repeated to the width, so the text holds three zeros.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 HIGH-F PIC X(2) VALUE HIGH-VALUE. PROCEDURE DIVISION. DISPLAY FUNCTION HEX-OF(HIGH-F). STOP RUN.

    Answer

    FFFF

    HIGH-VALUE is the byte FF in every position, so two bytes give four hex digits of F.

  9. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 78 MAX-ITEMS VALUE 3. 01 COUNTER-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. MOVE 5 TO MAX-ITEMS. DISPLAY MAX-ITEMS. STOP RUN.

    Answer

    error: invalid MOVE target: 3

    The name is replaced by 3 before the statement is checked, so the compiler sees MOVE 5 TO 3 and reports the target it found.

  10. What is the first line of the compiler output for this program? IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 78 BAD-CONST VALUE 10 PIC 9(2). PROCEDURE DIVISION. DISPLAY BAD-CONST. STOP RUN.

    Answer

    error: syntax error, unexpected PICTURE

    The parser meets the picture keyword where the entry should end and names the token it found.

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