Thursday, April 9, 2015


When Defining the cobol variables, the first thing which we see around is the USAGE clause. It only signifies how the data item will be internally stored. What will be its size.? In most instances we would see that a variable has been declared as(just for example) PIC X(10), and there is no USAGE clause. When the usage clause is not specified ,then it is assumed by the compiler that the USAGE IS DISPLAY.This is the default.
The other kind of USAGE is COMPUTATIONAL. As the name suggests, this is used when we want to perform numeric operations on the data items. I found some of the points to be worth remembering. Will be highlighting them in Red in this post.

Can we use USAGE IS DISPLAY for NUMERIC items? 
For text items, or for numeric items that are not going to be used in a computation (like Phone Numbers,postal code), the USAGE IS DISPLAY will not be harmful; but for numeric items upon which some arithmetic operation will be performed,the default usage is definitely not the best way.
When we use numeric items (like pic 9(3) )with default usage, internally they will be stored in ASCII.
When performing operations on these items, computer need to convert them to binary.Once done, computer need to convert it back to ASCII. These conversions unnecessarily slows down performance. Hence COMP is preferable in these scenarios.

Further more, Computational Usage clause can be classified into the following fields depending on the numeric items we use in our program.
Internal floating-point (USAGE COMP-1, USAGE COMP-2)
Internal decimal (USAGE PACKED-DECIMAL or COMP-3)

The maximum length of a comp item is 18 digits.

Q.Why do we use COMP in usage clause. When to use COMP ?
We know computers are Binary machines. So binary numbers will be more favorite to computers for doing calculations.Hence always use COMP for the variables which will be involved in numeric calculations.
Also it is used  for defining internal counters, subscripts.

PIC Clause of a COMP data item will only contain 9 or S.
 01 WS-LEN          PIC S9(02)  COMP.

Now, when we declare the variables in comp, the compiler allocates spaces to them in words.which can be a half word(2 bytes),  Full word(4 bytes), Double word(8 bytes)

So if the PIC specified is 9(1) COMP or 9(2) COMP or 9(3) or  COMP OR 9(4) COMP, the space allocated by the compiler will be half word (2 bytes).

From PIC 9(5) COMP to PIC 9(9) COMP, the space allocated will be 4 bytes

For a PIC clause with more than 9 bytes, compiler will allocate  double word(8 bytes).

In short,
S9(01) - S9(04)      Half word.
S9(05) - S9(09)      Full word.
S9(10) -  S9(18)     Double word.

Q.When to use COMP-1 and COMP-2 ? 
When we want to use Fractional numbers, numbers having decimal digits, then COMP-1 is the choice.Thus USAGE IS COMP-1 is used to store floating point numbers(real numbers)
COMP-1 uses 4 bytes of storage.
Exactly in the same way, we need to use COMP-2 for extremely big numbers requiring high precision.
COMP-2 occupies 8 bytes of storage. Hence no need to use PIC clause since size is already pre defined.
Remember: No picture clause is needed when defining COMP-1 and COMP-2 items.

Read COMP3 Variables Here.

No comments:

Post a Comment