COBOL Data types
COBOL really only has two data types: Numbers and strings.
The layout of each field in a COBOL record is precisely specified by a PICTURE
(usually abbreviated PIC
) clause. The most common ones are:
PIC X
for strings.PIC X(100)
means a 100-byte string.PIC 9
for numbers, optionally withS
(sign) orV
(implicit decimal point). For example,PIC S9(7)V99
means a signed number with 7 digits to the left of the implicit decimal point and 2 digits to the right.
Numeric fields can have a USAGE
clause to optimize their storage. The most common USAGE
s are DISPLAY
, COMP
, and COMP-3
.
DISPLAY
stores each digit as a character. For example, PIC 9(4) VALUE 123
stores the number as if it were the string "0123". And PIC 9(4)V99 VALUE 123.45
stores it as "012345". Note that the decimal point is not actually stored.
This is an inefficient format in that it requires 8 bits to represent each digit. But it does have an "optimization" for signed numbers by using half of the last byte to store the sign. Normally, EBCDIC digits all have a high nybble of F, so 0123 is F0 F1 F2 F3. But -0123 is F0 F1 F2 D3; the D indicates negative. C means positive, and F means unsigned (i.e., positive). (Similar formats are used in ASCII versions of COBOL, but not as standardized.)
COMP-3
is binary-coded decimal with trailing sign nybble. PIC 9(3) COMP-3 VALUE 123
becomes the two bytes 12 3F.
COMP
or BINARY
is native binary format, just like short
, int
, or long
in C.
As for deciding which data type to use, it can be made very complicated - BUT - a simple set of guidelines are:
DISPLAY and Edited Zone Decimal should only be used for displaying numerics in a report or sysout. Move COMP and COMP-3 fields to a DISPLAY/Edited field before putting it in a report or to sysout.
COMP - has the fastest calculation speed for integers
COMP-3 (PACKED Decimal) - should be used when decimal positions should be maintained.
COMP and COMP-3 fields can be used together in calculations. The compiler will determeine which field type will be converted (under the covers) to a single common numeric data type - rules based.
USAGE
in COBOL describes how a data item is to be used. A few examples
of USAGE are:
- DISPLAY. This identifies an item that may be printed on a terminal or
report. This may or may not be a number (e.g. could be a text value). The
description of the DISPLAY item is given by the PICture clause. For example:
PIC 9(5) USAGE DISPLAY
describes a 5 digit number that may be displayed (printed). OftenUSAGE DISPLAY
is left off because it is implied if missing. - INDEX. This identifies an item used as an index into a table (OCCURS).
- COMPsomething indicates that the data item is to be used in arithmetic operations (i.e. it is a number of some type).
There are various types of numeric item. Two of the most commonly used numeric data types are:
- COMPUTATIONAL or COMP. This is equivalent to BINARY
- COMPUTATIONAL-3 or COMP-3. This is equivalent to PACKED-DECIMAL
COMP (BINARY) data items are generally the most efficient way to perform calculations on data items that represent integer values.
COMP-3 (PACKED-DECIMAL) data items are used in COBOL because they maintain a fixed number of decimal points. All computations lead to a result having the prescribed number of decimal points. This is particularly useful in accounting type operations. Floating point numbers make the number of digits after the decimal point variable (e.g. the decimal point can "float") which is not the way financial operations are usually represented.
You can find a complete list of COMPutational items for IBM Enterprise COBOL here
One of the problems many programmers have when beginning with COBOL is understanding that a COMP item is great for doing math but cannot be displayed (printed) until it is converted into a DISPLAYable item through a MOVE statement. If you MOVE a COMP item into a report or onto a screen it will not present very well. It needs to be moved into a DISPLAY item first.
The other thing that you may want to research a bit more is the relationship between the PICture and the USAGE when defining variables in COBOL. Here is a link to a very good introductory COBOL Tutorial from the University of Limerick.