COBOL PICTURE Clauses
The PICTURE clause (almost always abbreviated PIC) is a template that describes the type and size of an
elementary item, one symbol per character position. It replaces the data types of other languages.
The basic symbols
| Symbol | Meaning | Example | Holds |
|---|---|---|---|
9 |
A decimal digit | PIC 999 |
000 to 999 |
X |
Any character | PIC XXX |
"A1-" |
A |
A letter or space | PIC AAA |
"Bob" |
S |
The item is signed (not a character position) | PIC S99 |
-99 to +99 |
V |
Implied decimal point (not stored) | PIC 99V99 |
12.34 stored as 1234 |
P |
Assumed zero digit (scaling, rare) | PIC 99PP |
1200 |
A number in parentheses repeats the symbol: 9(5) is 99999 and X(30) is thirty Xs. S9(7)V99 is a signed
number with seven integer digits and two decimals.
Three kinds of elementary items
- Numeric (
9,S,V,P): can take part in arithmetic. Max 38 digits in GnuCOBOL (18 in older standards). - Alphanumeric (
X, or a mix withX): any text, no arithmetic. - Alphabetic (
Aonly): letters and spaces. Rarely used; most text fields areX.
IDENTIFICATION DIVISION.
PROGRAM-ID. PIC-BASICS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-QUANTITY PIC 9(4) VALUE 25.
01 WS-PRODUCT PIC X(10) VALUE "Widget-7".
01 WS-SURNAME PIC A(8) VALUE "Hopper".
01 WS-TEMPERATURE PIC S9(3) VALUE -12.
01 WS-PRICE PIC 9(4)V99 VALUE 19.99.
PROCEDURE DIVISION.
DISPLAY "Quantity: " WS-QUANTITY
DISPLAY "Product: [" WS-PRODUCT "]"
DISPLAY "Surname: [" WS-SURNAME "]"
DISPLAY "Temperature: " WS-TEMPERATURE
DISPLAY "Price: " WS-PRICE
STOP RUN.Quantity: 0025
Product: [Widget-7 ]
Surname: [Hopper ]
Temperature: -012
Price: 0019.99
The implied decimal point V
V marks where the decimal point is without storing a . character. PIC 9(4)V99 occupies 6 bytes, not 7.
Arithmetic aligns the decimal points automatically. GnuCOBOL's DISPLAY shows the point so you can read the
value, but the stored data contains only digits. The function LENGTH reports the size in bytes:
IDENTIFICATION DIVISION.
PROGRAM-ID. V-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-RATE PIC 9V9999 VALUE 0.0425.
01 WS-AMOUNT PIC 9(5)V99 VALUE 1000.
01 WS-INTEREST PIC 9(5)V99.
PROCEDURE DIVISION.
MULTIPLY WS-AMOUNT BY WS-RATE GIVING WS-INTEREST
DISPLAY "Rate: " WS-RATE
DISPLAY "Interest: " WS-INTEREST
DISPLAY "Bytes in WS-AMOUNT: " FUNCTION LENGTH(WS-AMOUNT)
STOP RUN.Rate: 0.0425
Interest: 00042.50
Bytes in WS-AMOUNT: 7
Signed and unsigned
Without S, a numeric item is unsigned: storing a negative result silently drops the sign. This is one of the
most common COBOL bugs, so give every item that can go negative an S:
IDENTIFICATION DIVISION.
PROGRAM-ID. SIGN-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-UNSIGNED PIC 9(3) VALUE 10.
01 WS-SIGNED PIC S9(3) VALUE 10.
PROCEDURE DIVISION.
SUBTRACT 25 FROM WS-UNSIGNED
SUBTRACT 25 FROM WS-SIGNED
DISPLAY "Unsigned 10 - 25 = " WS-UNSIGNED
DISPLAY "Signed 10 - 25 = " WS-SIGNED
STOP RUN.Unsigned 10 - 25 = 015
Signed 10 - 25 = -015
Size limits and truncation
A numeric item holds exactly the digits declared. If a result has more integer digits, the high-order digits are
cut off (unless you use ON SIZE ERROR, covered later). Text that is too long for an X field is cut on the
right:
IDENTIFICATION DIVISION.
PROGRAM-ID. TRUNC-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-SMALL PIC 99 VALUE 95.
01 WS-CODE PIC X(4).
PROCEDURE DIVISION.
ADD 10 TO WS-SMALL
MOVE "ABCDEFG" TO WS-CODE
DISPLAY "95 + 10 in PIC 99: " WS-SMALL
DISPLAY "ABCDEFG in X(4): " WS-CODE
STOP RUN.95 + 10 in PIC 99: 05
ABCDEFG in X(4): ABCD
Tip: Choose sizes from the business rules: "an account balance is at most 9,999,999.99" gives
PIC S9(7)V99. Too small and values are truncated; much too large just wastes record space.
Test yourself
How many bytes does PIC S9(5)V99 occupy in DISPLAY usage?
- 5
- 7
- 8
- 9
What happens when you store -15 in PIC 9(3)?
- A runtime error
- It stores 015 (the sign is lost)
- It stores -15
- It stores 985
Which picture holds a price such as 1234.56?
- PIC X(7)
- PIC 9(4).99
- PIC 9(4)V99
- PIC 9(6)