COBOL Data types

Manasi picture Manasi · Jun 8, 2010 · Viewed 54.1k times · Source

I have confusion regarding COBOL data types. Like in many interviews it is asked to explain the difference between COMP-3 and COMP... what is the exact difference? what is the meaning of usage modes in COBOL and how is it related to data types?

Answer

NealB picture NealB · Jun 8, 2010

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). Often USAGE 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.