VALUE clauses and the initial state
A field does not start empty. Every elementary item may carry a VALUE clause in the data division, and the clause is applied before the first statement of the procedure division runs, so a program can read a field it has never written.
The first value of a field
The clause takes one literal: characters in quotes for an alphanumeric field, a number without them for a numeric one. Five fields, five clauses, one program:
IDENTIFICATION DIVISION.
PROGRAM-ID. VAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N-CODE PIC 9(3) VALUE 7.
01 NEG-CODE PIC S9(3) VALUE -42.
01 TOWN PIC X(8) VALUE "LONDON".
01 SHORT PIC X(5) VALUE "AB".
01 RATE PIC 9(3)V99 VALUE 12.5.
PROCEDURE DIVISION.
DISPLAY "[" N-CODE "]".
DISPLAY "[" NEG-CODE "]".
DISPLAY "[" TOWN "]".
DISPLAY "[" SHORT "]".
DISPLAY "[" RATE "]".
DISPLAY "RATE is " LENGTH OF RATE " bytes".
DISPLAY "NEG-CODE is " LENGTH OF NEG-CODE " bytes".
STOP RUN.
cobc -x -free -Wall -o val val.cob
./val
[007]
[-042]
[LONDON ]
[AB ]
[012.50]
RATE is 5 bytes
NEG-CODE is 3 bytes
Seven and twelve and a half came in as short numbers and left as the full width of their pictures. That is the whole of the clause: the value is stored in the field, not printed as it was written, and the picture has the last word on what the bytes look like.
GnuCOBOL also accepts a VALUE on the 01 itself and lays the characters over the children: a record of two PIC X fields with VALUE "AB" prints AB. This course puts the clause on each elementary item, where the picture is, and never relies on the group form.
The picture pads and fills
A literal shorter than the picture is stretched, and the direction depends on the category of the field. A numeric field is padded on the left with zeros, because that is how a number keeps its value: 7 in a PIC 9(3) is 007. An alphanumeric field is padded on the right with spaces, because the text starts at the first position: "AB" in a PIC X(5) is AB and three spaces.
The V in PIC 9(3)V99 takes no byte, so the field is five bytes and holds 01250. The point is stored in the picture alone, and DISPLAY prints the field with the point in it: 012.50. A literal longer than the picture is cut, on the right in an alphanumeric field and at the high-order end in a numeric one, and the build warns about it; the chapter on what the clause refuses shows the warning.
The sign is the same kind of agreement between the picture and the bytes. PIC S9(3) is still three bytes for three digits, the minus is not one of them, and DISPLAY prints it in front of the digits. Where the sign really sits is the subject of the lesson on SIGN.
VALUE ALL
Four fields of a banner want the same character over and over, and VALUE ALL writes it once:
IDENTIFICATION DIVISION.
PROGRAM-ID. VALL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STAR-LINE PIC X(5) VALUE ALL "*".
01 DASHES PIC X(4) VALUE ALL "-".
01 BLANKS PIC X(6) VALUE ALL SPACE.
01 MIXED PIC X(6) VALUE ALL "AB".
PROCEDURE DIVISION.
DISPLAY "[" STAR-LINE "]".
DISPLAY "[" DASHES "]".
DISPLAY "[" BLANKS "]".
DISPLAY "[" MIXED "]".
STOP RUN.
cobc -x -free -Wall -o vall vall.cob
./vall
[*****]
[----]
[ ]
[ABABAB]
ALL repeats the literal until the field is full, and the literal may be longer than one character: ALL "AB" in six bytes is ABABAB. SPACE is a figurative constant and needs no quotes, so a field of six spaces is a clause rather than six characters to count.
A field with no VALUE
A clause is optional, and a field without one still starts with something well defined: spaces if it is alphanumeric, zeros if it is numeric. Nothing is left over from a previous run, because there is no previous run: the program image is loaded fresh, and its working storage is part of the image.
IDENTIFICATION DIVISION.
PROGRAM-ID. FRESH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 REC.
05 A-FIELD PIC X(4).
05 B-FIELD PIC 9(3).
05 C-FIELD PIC S9(2)V9.
01 N-COUNT PIC 9(2).
PROCEDURE DIVISION.
DISPLAY "[" A-FIELD "][" B-FIELD "][" C-FIELD "]".
DISPLAY "LENGTH OF REC is " LENGTH OF REC.
ADD 1 TO N-COUNT.
DISPLAY "counter after ADD 1: [" N-COUNT "]".
STOP RUN.
cobc -x -free -Wall -o fresh fresh.cob
./fresh
[ ][000][+00.0]
LENGTH OF REC is 10
counter after ADD 1: [01]
The counter proves the zeros are real digits: one ADD turns 00 into 01, while a field of spaces would have stopped the build. The length is ten bytes: 4, 3 and 3.
Do not read this as "a field is empty". A field of spaces is not a null and not a zero: a comparison with ZERO fails on it, and a MOVE of it into a numeric field is accepted without a word and leaves 000, which is a value nobody wrote. The lesson on alphanumeric fields takes the comparison apart.
INITIALIZE
A record used by a hundred transactions has to go back to a known state at the start of each. INITIALIZE is the statement for that, and its state is not the state the VALUE clauses describe: it sets every field to the default of its category, spaces for alphanumeric and zeros for numeric.
IDENTIFICATION DIVISION.
PROGRAM-ID. INIT.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 CUST-ID PIC 9(4) VALUE 101.
05 CUST-NAME PIC X(8) VALUE "ADA".
05 CUST-BAL PIC S9(5)V99 VALUE -12.5.
PROCEDURE DIVISION.
DISPLAY "fresh [" CUST-ID "][" CUST-NAME "][" CUST-BAL "]".
MOVE 9999 TO CUST-ID.
MOVE "GRACE" TO CUST-NAME.
MOVE 88.25 TO CUST-BAL.
DISPLAY "moved [" CUST-ID "][" CUST-NAME "][" CUST-BAL "]".
INITIALIZE CUSTOMER.
DISPLAY "init [" CUST-ID "][" CUST-NAME "][" CUST-BAL "]".
STOP RUN.
cobc -x -free -Wall -o init init.cob
./init
fresh [0101][ADA ][-00012.50]
moved [9999][GRACE ][+00088.25]
init [0000][ ][+00000.00]
The first line is the VALUE clauses at work before any statement. The third line is the reason for the chapter: the name is spaces again, the identifier is zeros, and the identifier is not 0101. INITIALIZE knows nothing about the clauses; it writes the default of the category over whatever is there.
That is not a flaw to work around but the rule to remember: a program that needs the VALUE back has to MOVE it, and a program that expects a fresh record from INITIALIZE is wrong in every case where a field has a value that is not the category default. A level 88 condition name is not touched at all, since it takes no storage.
INITIALIZE REPLACING
The default is often not what the record wants. A blank row has to look blank on a report, and a zero identifier has to be visible as missing, so INITIALIZE takes a REPLACING list: each category, and the value to put in its place.
IDENTIFICATION DIVISION.
PROGRAM-ID. REPL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ROW.
05 ROW-ID PIC 9(3) VALUE 101.
05 ROW-NAME PIC X(6) VALUE "LONDON".
05 ROW-CODE PIC X(2) VALUE "AB".
PROCEDURE DIVISION.
INITIALIZE ROW REPLACING ALPHANUMERIC BY "*" NUMERIC BY 9.
DISPLAY "[" ROW-ID "][" ROW-NAME "][" ROW-CODE "]".
STOP RUN.
cobc -x -free -Wall -o repl repl.cob
./repl
[009][* ][* ]
A numeric field is filled with 9, so 101 becomes 009 and not 999: the replacing literal is padded by the picture exactly as a VALUE would be. The alphanumeric fields get the star in every position, because a replacing value of one character is repeated to the width of the field.
What the clause refuses
Three mistakes cover the clause. A literal longer than the picture is not refused, and that is the first surprise: the build warns and the field keeps the first bytes.
IDENTIFICATION DIVISION.
PROGRAM-ID. TOOLONG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TOWN PIC X(3) VALUE "LONDON".
PROCEDURE DIVISION.
DISPLAY "[" TOWN "]".
STOP RUN.
cobc -x -free -Wall -o toolong toolong.cob
./toolong
toolong.cob:6: warning: value size exceeds data size [-Wothers]
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 TOWN PIC X(3) VALUE "LONDON".
7 |
8 | PROCEDURE DIVISION.
toolong.cob:6: note: value size is 6 [-Wothers]
[LON]
The program runs with LON in the field. A warning is not a guard: the value is cut from the right, silently, and only the text of the build says that something was lost.
The second mistake is a value that is not a literal. A field cannot take its value from another field in the data division, because no statement has run yet:
IDENTIFICATION DIVISION.
PROGRAM-ID. VALFIELD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BASE PIC 9(3) VALUE 500.
01 OTHER PIC 9(3) VALUE BASE.
PROCEDURE DIVISION.
DISPLAY OTHER.
STOP RUN.
cobc -x -free -Wall -o valfield valfield.cob
valfield.cob:7: error: syntax error, unexpected OTHER
5 | WORKING-STORAGE SECTION.
6 | 01 BASE PIC 9(3) VALUE 500.
7 > 01 OTHER PIC 9(3) VALUE BASE.
8 |
9 | PROCEDURE DIVISION.
valfield.cob:10: error: syntax error, unexpected OTHER, expecting (
8 |
9 | PROCEDURE DIVISION.
10 > DISPLAY OTHER.
11 | STOP RUN.
12 | <EOF>
Read the line numbers with care. The clause is on line 7 and the first message is reported at line 7, but the token it names, OTHER, is the next entry: the parser only knows that the clause had no literal when it reaches the token after it. The second error is the consequence, and it goes away with the first. This is the shape of most COBOL syntax errors: the message names what the parser found, not what you meant.
The third mistake is VALUE ALL with a number. The clause repeats a character, so its literal is the alphanumeric kind, and a numeric field is refused even though the same number would be fine without ALL:
IDENTIFICATION DIVISION.
PROGRAM-ID. VALLNUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DIGITS PIC 9(4) VALUE ALL 7.
PROCEDURE DIVISION.
DISPLAY DIGITS.
STOP RUN.
cobc -x -free -Wall -o vallnum vallnum.cob
vallnum.cob:6: error: invalid VALUE clause
4 | DATA DIVISION.
5 | WORKING-STORAGE SECTION.
6 > 01 DIGITS PIC 9(4) VALUE ALL 7.
7 |
8 | PROCEDURE DIVISION.
For a field of four sevens the clause is VALUE 7777. ALL belongs to the alphanumeric field, where the character it repeats is unambiguous.
Exercises
- Change PIC X(5) VALUE "AB" to PIC 9(5) VALUE 12 and explain the two outputs from the picture alone.
- Print LENGTH OF the customer record of the INITIALIZE program before and after INITIALIZE. What changes, and what does not?
- Give CUST-BAL a PIC 9(5)V99 instead of PIC S9(5)V99, build, and copy the warning.
- Initialize the row again with REPLACING NUMERIC BY 0 only. What happens to the alphanumeric fields, and why?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
When does a VALUE clause take effect?
Answer
Before the first statement of the procedure division runs
The clause is part of the data division: it describes the bytes of the program image, so the field holds the value from the first statement on.
-
What does a numeric field without VALUE hold at the start of a run?
Answer
Zeros
Working storage is part of the program image, and a numeric field starts at zero; an alphanumeric field starts as spaces.
-
What does INITIALIZE do to a field that has a VALUE clause?
Answer
It writes the default of the field category, not the VALUE
INITIALIZE is about categories: spaces for alphanumeric, zeros for numeric. The VALUE clause is not a template it can replay.
-
What does
VALUE ALL "AB"put into aPIC X(6)field?Answer
ABABAB
ALL repeats the whole literal until the field is full, so the pair fills six bytes three times.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 N-CODE PIC 9(3) VALUE 7. PROCEDURE DIVISION. DISPLAY "[" N-CODE "]". STOP RUN.Answer
[007]
A numeric field is padded on the left with zeros, so the one digit becomes the three digits of the picture.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 DASHES PIC X(4) VALUE ALL "-". PROCEDURE DIVISION. DISPLAY "[" DASHES "]". STOP RUN.Answer
[----]
ALL repeats the literal to the width of the field, so four characters fill the four bytes.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 C. 05 ID-F PIC 9(3) VALUE 101. 05 NM-F PIC X(4) VALUE "ADA ". PROCEDURE DIVISION. INITIALIZE C. DISPLAY "[" ID-F "][" NM-F "]". STOP RUN.Answer
[000][ ]
INITIALIZE writes the category default over the VALUE: zeros in the numeric field, spaces in the alphanumeric one.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 BASE PIC 9(3) VALUE 500. 01 OTHER PIC 9(3) VALUE BASE. PROCEDURE DIVISION. DISPLAY OTHER. STOP RUN.Answer
error: syntax error, unexpected OTHER
A VALUE takes a literal, not a field name. The parser knows the clause has no literal only at the next entry, so the message names that entry.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 TOWN PIC X(3) VALUE "LONDON". PROCEDURE DIVISION. DISPLAY "[" TOWN "]". STOP RUN.Answer
[LON]
The build warns that the value is longer than the field and cuts it from the right, so the three bytes hold LON.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 DIGITS PIC 9(4) VALUE ALL 7. PROCEDURE DIVISION. DISPLAY DIGITS. STOP RUN.Answer
error: invalid VALUE clause
ALL repeats a character, so its literal is alphanumeric and a numeric field refuses the clause; VALUE 7777 is the way to write it.
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