Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Divisions and Data Description

USAGE and VALUE: DISPLAY, COMP, COMP-3 and Initial Values

Understand COBOL storage formats: USAGE DISPLAY, BINARY/COMP, packed decimal COMP-3, COMP-1/COMP-2, sign handling, VALUE clauses and how many bytes each format needs.

A PIC clause says what a field means. The USAGE clause says how the computer stores it. The same picture S9(5) can occupy five bytes of printable digits, two to four bytes of binary, or three bytes of packed decimal. Choosing the format changes file sizes and speed, and when you read a mainframe file you must know which format its fields use.

The common usages

Usage Also written Meaning Size for S9(5)
DISPLAY (default) One character per digit, readable text 5 bytes
BINARY COMP, COMP-4, COMP-5 Two's-complement binary integer 4 bytes
PACKED-DECIMAL COMP-3 Two digits per byte, sign in the last half-byte 3 bytes
single float COMP-1 4-byte floating point 4 bytes
double float COMP-2 8-byte floating point 8 bytes
COBOL
       01 COUNTER-A    PIC 9(5).
       01 COUNTER-B    PIC 9(5) USAGE COMP.
       01 AMOUNT       PIC S9(7)V99 COMP-3.

Floating-point usages exist, but they bring back the rounding problems COBOL avoids. Use them only for scientific values; money belongs in DISPLAY or COMP-3.

DISPLAY

Every digit is a character. It is easy to print, read in a hex dump and move between programs. It is also the slowest for arithmetic, because the machine must convert it. A signed DISPLAY number normally stores the sign in the last byte, e.g. -123 as 12L in EBCDIC-style overpunch or 12s in ASCII-style, unless SIGN IS TRAILING SEPARATE CHARACTER is given, which adds one byte holding a real + or -.

BINARY (COMP)

The number of bytes depends on the number of digits in the picture. On IBM compilers (and in GnuCOBOL with its IBM-style settings, -std=ibm):

Digits Bytes
1-4 2 (halfword)
5-9 4 (fullword)
10-18 8 (doubleword)

Binary is compact and fast for integers and subscripts. But byte order depends on the machine (mainframes are big-endian) and a value like 99999 in PIC 9(4) COMP may be accepted or truncated depending on a compiler setting (TRUNC on IBM, -fbinary-truncate in GnuCOBOL). COMP-5 is native binary with no digit truncation.

PACKED DECIMAL (COMP-3)

Packed decimal is the mainframe workhorse. Each digit takes one nibble (4 bits). The last nibble holds the sign: C for plus, D for minus, F for unsigned. A field with n digits needs (n + 2) // 2 bytes (integer division). The number of nibbles is always even, so a leading 0 pads odd digit counts.

+12345 stored in PIC S9(5) COMP-3 is the three bytes 12 34 5C. -123 in PIC S9(3) COMP-3 is 12 3D. If you open a mainframe file in a text editor and see unreadable characters in a numeric field, it is packed.

Python Packed decimal encoder (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How many bytes?

Python Storage size by usage (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For a record with 20 numeric fields of 9 digits, packed decimal saves about 4 bytes each: that is 80 bytes per record and 80 MB per million records.

The VALUE clause

VALUE sets the initial contents of a working-storage item.

COBOL
       01 GREETING     PIC X(12)    VALUE "Hello".
       01 MAX-ITEMS    PIC 9(3)     VALUE 100.
       01 RATE         PIC V999     VALUE .075.
       01 STARTING-BAL PIC S9(5)V99 VALUE -250.00.
       01 FLAG-LINE    PIC X(10)    VALUE ALL "-".
       01 TABLE-HEAD   PIC X(5)     VALUE SPACES.
       01 TOTAL        PIC 9(6)     VALUE ZERO.

Rules: the literal must agree with the picture (numbers in numeric items, text in X items), a text literal shorter than the field is padded with spaces, and VALUE is not allowed in the file section (records get their contents from the file) or the linkage section. In the LOCAL-STORAGE section VALUE is applied on every call. For a fixed constant, use a normal item with VALUE and never change it; COBOL 2002 also offers a CONSTANT form: 01 PI CONSTANT AS 3.14159.

SYNCHRONIZED and SIGN

  • SYNCHRONIZED (or SYNC) aligns a binary item on its natural word boundary, which can add slack bytes inside a record. It is a classic cause of record length mismatches.
  • SIGN IS LEADING|TRAILING [SEPARATE CHARACTER] controls how the sign of a DISPLAY number is stored.

Common mistakes

  • Reading a COMP-3 field as text in a file transfer. Mainframe files must be moved in binary mode, or the packed bytes are corrupted by code-page conversion.
  • Using COMP on a record layout shared with another system without checking byte order and size rules.
  • Declaring PIC 9(4) COMP and storing 12345; the result depends on compiler options.
  • VALUE on a group item with a non-alphanumeric child: the literal initialises the group as text only.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many bytes does PIC S9(7) COMP-3 occupy?

    (7 + 2) // 2 = 4 bytes: seven digit nibbles plus one sign nibble fit exactly into 4 bytes.
  2. 2. What does the last nibble C mean in a packed decimal field?

    C is the positive sign, D is negative and F means unsigned.
  3. 3. Which usage stores one printable character per digit?

    DISPLAY (the default) stores digits as characters. COMP is binary and COMP-3 is packed.
  4. 4. Why should money not be stored in COMP-2?

    COMP-1 and COMP-2 are binary floating point; values such as 0.10 are approximations. Use DISPLAY or COMP-3.

Summary

  • USAGE controls how a field is stored; PIC controls what it means.
  • DISPLAY is readable text; COMP is binary (2/4/8 bytes by digits); COMP-3 is packed decimal (digits/2 + 1 bytes, sign nibble C/D/F).
  • Floating COMP-1/COMP-2 should not hold money.
  • VALUE gives initial contents in working storage; literals must match the picture.

Next: REDEFINES, RENAMES and Condition Names