USAGE COMP and COMP-5
A binary field holds the number the way a processor does: in two, four or eight bytes, not one character per digit. COBOL has had it since the 1960s under the name COMP, and GnuCOBOL has the native variant COMP-5 next to it. The difference between the two is one line of arithmetic that each of them gets wrong in a different way.
A number in two or four bytes
The size of a COMP field follows the number of digits in its picture, because the picture says how large the number may be: up to four digits fit in two bytes, up to nine in four bytes, and up to eighteen in eight.
IDENTIFICATION DIVISION.
PROGRAM-ID. SIZES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 D-3 PIC 9(3).
01 C-3 PIC 9(3) COMP.
01 C-5 PIC 9(5) COMP.
01 C-9 PIC 9(9) COMP.
01 C-10 PIC 9(10) COMP.
01 C-18 PIC 9(18) COMP.
01 S-3 PIC S9(3) COMP.
01 F-3 PIC 9(3) COMP-5.
01 P-3 PIC S9(5)V99 COMP-3.
PROCEDURE DIVISION.
DISPLAY "DISPLAY 9(3) " LENGTH OF D-3.
DISPLAY "COMP 9(3) " LENGTH OF C-3.
DISPLAY "COMP 9(5) " LENGTH OF C-5.
DISPLAY "COMP 9(9) " LENGTH OF C-9.
DISPLAY "COMP 9(10) " LENGTH OF C-10.
DISPLAY "COMP 9(18) " LENGTH OF C-18.
DISPLAY "COMP S9(3) " LENGTH OF S-3.
DISPLAY "COMP-5 9(3) " LENGTH OF F-3.
DISPLAY "COMP-3 S9(5)V99 " LENGTH OF P-3.
STOP RUN.
cobc -x -free -Wall -o sizes sizes.cob
./sizes
DISPLAY 9(3) 3
COMP 9(3) 2
COMP 9(5) 4
COMP 9(9) 4
COMP 9(10) 8
COMP 9(18) 8
COMP S9(3) 2
COMP-5 9(3) 2
COMP-3 S9(5)V99 4
Ten five-digit numbers take fifty bytes in DISPLAY and forty here, and ten nine-digit ones ninety against forty. That ratio is why every mainframe file format has a binary convention in it somewhere. The sign costs nothing in COMP: it is the top bit of the number rather than a character.
Two byte orders
A number of two bytes is one byte after the other, and the order is a convention. COMP follows the IBM convention, high byte first; COMP-5 follows the machine, low byte first on x86. The same 258 is 0102 in one and 0201 in the other, and FUNCTION HEX-OF prints the bytes:
IDENTIFICATION DIVISION.
PROGRAM-ID. BINARY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 C-4 PIC 9(4) COMP.
01 F-4 PIC 9(4) COMP-5.
01 D-4 PIC 9(4).
PROCEDURE DIVISION.
MOVE 258 TO D-4.
MOVE 258 TO C-4.
MOVE 258 TO F-4.
DISPLAY "DISPLAY 9(4)=258 hex " FUNCTION HEX-OF(D-4).
DISPLAY "COMP 9(4)=258 hex " FUNCTION HEX-OF(C-4).
DISPLAY "COMP-5 9(4)=258 hex " FUNCTION HEX-OF(F-4).
DISPLAY "COMP value [" C-4 "]".
DISPLAY "COMP-5 value [" F-4 "]".
STOP RUN.
cobc -x -free -Wall -o binary binary.cob
./binary
DISPLAY 9(4)=258 hex 30323538
COMP 9(4)=258 hex 0102
COMP-5 9(4)=258 hex 0201
COMP value [0258]
COMP-5 value [00258]
The DISPLAY field shows eight hex digits because its four bytes are the four characters 0, 2, 5, 8, which is 30 32 35 38 in hex. The two binary fields show two bytes each, and the reversal is the whole difference between 0102 and 0201.
The last line has a surprise in it: the COMP-5 field prints five characters, 00258, where the COMP field prints four. A COMP-5 field is a native binary integer, and two bytes can hold up to 65535, so its output width comes from the storage and not from the four digits of the picture.
The V is still there
A binary field with an implied decimal point has no point; it holds the number multiplied by ten for each decimal place, and the picture tells every statement where the point is. Twelve and thirty-four hundredths is 1234 in the field:
IDENTIFICATION DIVISION.
PROGRAM-ID. SCALED.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 PRICE-C PIC 9(3)V99 COMP VALUE 12.34.
01 PRICE-D PIC 9(3)V99 VALUE 12.34.
PROCEDURE DIVISION.
DISPLAY "COMP hex " FUNCTION HEX-OF(PRICE-C) " bytes " LENGTH OF PRICE-C.
DISPLAY "DISP hex " FUNCTION HEX-OF(PRICE-D) " bytes " LENGTH OF PRICE-D.
DISPLAY "COMP value [" PRICE-C "]".
ADD 0.01 TO PRICE-C.
DISPLAY "after +0.01 [" PRICE-C "]".
STOP RUN.
cobc -x -free -Wall -o scaled scaled.cob
./scaled
COMP hex 000004D2 bytes 4
DISP hex 3031323334 bytes 5
COMP value [012.34]
after +0.01 [012.35]
04D2 is 1234, so the field holds cents and the picture remembers where to put the point back. The DISPLAY field is five characters of text, 30 31 32 33 34. Both are decimal to the program: an ADD of a cent changes the last digit in both, and neither of them goes through a float.
The digits of COMP are a limit
Here is where the two usages part. A COMP field is decimal in its range: PIC 9(4) holds four digits, and a value outside them is cut to four digits, silently at run time and with a warning at build time when the value is a literal. COMP-5 is decimal in nothing: it holds what its two bytes can hold, which is 65536 values.
IDENTIFICATION DIVISION.
PROGRAM-ID. OVER.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 C-4 PIC 9(4) COMP VALUE 9999.
01 F-4 PIC 9(4) COMP-5 VALUE 9999.
01 C-MOVE PIC 9(4) COMP.
01 F-MOVE PIC 9(4) COMP-5.
PROCEDURE DIVISION.
ADD 1 TO C-4.
DISPLAY "COMP 9999 + 1 = [" C-4 "]".
ADD 1 TO F-4.
DISPLAY "COMP-5 9999 + 1 = [" F-4 "]".
MOVE 123456 TO C-MOVE.
DISPLAY "COMP after MOVE 123456 = [" C-MOVE "]".
MOVE 123456 TO F-MOVE.
DISPLAY "COMP-5 after MOVE 123456 = [" F-MOVE "]".
STOP RUN.
cobc -x -free -Wall -o over over.cob
./over
over.cob:16: warning: value size exceeds data size [-Wtruncate]
14 | ADD 1 TO F-4.
15 | DISPLAY "COMP-5 9999 + 1 = [" F-4 "]".
16 > MOVE 123456 TO C-MOVE.
17 | DISPLAY "COMP after MOVE 123456 = [" C-MOVE "]".
18 | MOVE 123456 TO F-MOVE.
over.cob:16: note: value is 123456 [-Wtruncate]
over.cob:8: note: 'C-MOVE' defined here as PIC 9(4) [-Wtruncate]
6 | 01 C-4 PIC 9(4) COMP VALUE 9999.
7 | 01 F-4 PIC 9(4) COMP-5 VALUE 9999.
8 > 01 C-MOVE PIC 9(4) COMP.
9 | 01 F-MOVE PIC 9(4) COMP-5.
10 |
over.cob:18: warning: numeric literal exceeds data size [-Wtruncate]
16 | MOVE 123456 TO C-MOVE.
17 | DISPLAY "COMP after MOVE 123456 = [" C-MOVE "]".
18 > MOVE 123456 TO F-MOVE.
19 | DISPLAY "COMP-5 after MOVE 123456 = [" F-MOVE "]".
20 | STOP RUN.
COMP 9999 + 1 = [0000]
COMP-5 9999 + 1 = [10000]
COMP after MOVE 123456 = [3456]
COMP-5 after MOVE 123456 = [57920]
Read the four lines one by one. COMP wrapped at ten thousand because four digits are all its picture promises, and the sum of the last four digits of 123456 is 3456. COMP-5 accepted ten thousand, which fits in two bytes, and then turned 123456 into 57920, which is 123456 minus 65536: the high half of the number had nowhere to go.
Neither behaviour is an error the program can see. If a value may leave the range of the picture, the statement that could overflow is the one with ON SIZE ERROR on it, which its own lesson covers; a test that relies on a warning in the build only covers the literals.
COMP-5 has no digit limit
COMP-5 is the usage for a number that comes from or goes to the machine: a C library, an operating system call, a file written by another program with native integers. It is the same two, four or eight bytes, in the order the processor uses, and its range is the range of those bytes and not of the picture.
That makes it the wrong choice for a business field. A quantity of PIC 9(4) is meant to stop at 9999, and a COMP-5 field of the same picture will happily hold 10000 and print five digits, as the run above shows. A report column sized for four characters is then one character short, and the next reader of the file sees a record that does not line up.
The compiler can help with the literals: the warning numeric literal exceeds data size on the MOVE 123456 is there because -Wall turned on -Wtruncate. For a value computed at run time there is no such help, and the check has to be in the program.
Which one to choose
- DISPLAY for anything a person or another system reads: a file record, a report line, a field that has to print as it is stored.
- COMP for counters, indexes and dates, where the value is whole and the digits of the picture are a promise the program keeps.
- COMP-5 at the edge of the machine: a CALL into C, a system interface, a binary file with a native integer in it.
- COMP-3 for a decimal number with many digits that has to stay exact and small; the next lesson takes it.
A record that leaves the program keeps the usage it was written with, so the usage is part of the file format. A reader that expects COMP and meets COMP-5 sees the right number only when the two bytes happen to be equal, and that is exactly the bug that takes a day to find.
Exercises
- Print LENGTH OF a PIC S9(9)V99 COMP and a PIC S9(10)V99 COMP. Which digit count crossed the boundary?
- Print the hex of PIC 9(4) COMP VALUE 1 and of the same value in COMP-5. Which end holds the one?
- Move 70000 into a PIC 9(5) COMP and into a PIC 9(5) COMP-5. One of them keeps the value; explain the other from 65536.
- Change C-4 to PIC 9(6) COMP and run the overflow program again. Why does 9999 + 1 now work?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many bytes does a
PIC 9(3) COMPfield take?Answer
2
Up to four digits fit in a two-byte binary integer, so the picture costs one byte less than the same number in DISPLAY.
-
Which picture crosses from four bytes to eight in COMP?
Answer
PIC 9(10)
Nine digits still fit in four bytes; ten need eight, and the sizes run 2, 4, 8 with nothing in between.
-
What is the difference between COMP and COMP-5?
Answer
COMP is high byte first and limited by the picture; COMP-5 is native order and limited by the storage
Both are binary; COMP follows the IBM byte order and the digit count of the picture, while COMP-5 follows the machine and the width of the storage.
-
Which usage belongs in a file record that another program reads as text?
Answer
DISPLAY
DISPLAY stores the characters themselves, so a reader that knows nothing about COBOL can read the record and print it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-4 PIC 9(4) COMP. PROCEDURE DIVISION. MOVE 258 TO C-4. DISPLAY FUNCTION HEX-OF(C-4). STOP RUN.Answer
0102
COMP writes the high byte first, and 258 is 0x0102: one, then two.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 F-4 PIC 9(4) COMP-5. PROCEDURE DIVISION. MOVE 258 TO F-4. DISPLAY FUNCTION HEX-OF(F-4). STOP RUN.Answer
0201
COMP-5 uses the byte order of the machine, so the low byte comes first: two, then one.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-4 PIC 9(4) COMP VALUE 9999. PROCEDURE DIVISION. ADD 1 TO C-4. DISPLAY "[" C-4 "]". STOP RUN.Answer
[0000]
The picture promises four digits, so the sum wraps at ten thousand and the residue is zero.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 F-4 PIC 9(4) COMP-5 VALUE 9999. PROCEDURE DIVISION. ADD 1 TO F-4. DISPLAY "[" F-4 "]". STOP RUN.Answer
[10000]
Two bytes hold up to 65535, and COMP-5 takes the storage as the range, so ten thousand is kept and printed in five characters.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 C-4 PIC 9(4) COMP. PROCEDURE DIVISION. MOVE 123456 TO C-4. DISPLAY "[" C-4 "]". STOP RUN.Answer
[3456]
COMP keeps the picture as the limit, so only the last four decimal digits survive and the build warns about the truncation.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 F-4 PIC 9(4) COMP-5. PROCEDURE DIVISION. MOVE 123456 TO F-4. DISPLAY "[" F-4 "]". STOP RUN.Answer
[57920]
COMP-5 does not cut to the picture: the value wraps at the two-byte limit, and 123456 minus 65536 is 57920.
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