COBOL - Usage Clause
The USAGE clause is used to specify how a data item is to be stored in the computer's memory. Every variable declared in a COBOL program has a USAGE clause - even when no explicit clause is specified. When there is no explicit USAGE clause, the default - USAGE IS DISPLAY - is applied.
-
The USAGE Clause can be specified on data items defined with any level number.
-
The USAGE of a group item is valid for all its sub items.
Normally, A computer can store data in more than one internal form. In COBOL, a programmer is allowed to specify the internal form of the data item so as to facilitate its use in the most efficient manner.
There are only two general forms of internal representation in COBOL,
-
COMPUTATIONAL
-
DISPLAY
Only numeric data items can be
specified as USAGE IS COMPUTATIONAL and the name itself suggests a
data item specified as USAGE IS COMPUTATIONAL can take part in
arithmetic operations more efficiently and any data item can be
specified as USAGE IS DISPLAY.
If we omit Usage clause compiler
assume DISPLAY as default.
In COMP usage Data is stored as Pure Binary format internally.
Depending on the size of the data item,
It can be stored either
in a half-word (2 bytes with range -32,768 to +32767) or full-
word (4 bytes with range -2,147,483,648 to 2,147,483,647).
The
PICTURE Clause of a COMPUTATIONAL data item should not contain
any character other than 9 or S.
Example:
01 WS-HDR-LEN PIC S9(02) USAGE COMP. |
This occupies 2 bytes and data stored as Pure binary format internally.
Data name length | Length in COBOL |
9(01) to 9(04) | 2 bytes |
9(05) to 9(09) | 4 bytes |
S9(10) to S9(18) | 8 bytes |
COMP-1: In this case the data item will be represented in one word in
the floating point form the number is actually represented in
hexadecimal format and is suitable for arithmetic operations.
The PICTURE Clause cannot be specified for COMP-1 items.
COMP-1: It takes 4 bytes of storage.
COMP-2: This is same as COMP-1, except that the data is represented
internally in two words. The advantage is that this increases
the precision of the data, which means that more significant
digits are available. Similar to COMP-1, The PICTURE Clause
cannot be specified for COMP-2 items also. COMP-1 takes 8
bytes of storage.COMP-2 is more precision than COMP-1.
COMP-3: In this case the data is represented in the decimal form, but
one digit takes half a byte. The sign is stored separately as
the rightmost half a byte character.
Find Comp-3 storage in bytes: Length of variable/2, if this is
not pure number then it takes immediate next number of bytes.
For Example: S9(06) takes 4 bytes.
Half byte for sign (S) and variable length is 6 bytes which takes 3 bytes. So the
total is 3 and half. so its immediate number is 4. Additional added half byte is called slack byte.
COMP-3 Picture clause | Number of bytes occupied by the field |
S9(1) COMP-3 | 1 |
S9(2) COMP-3 | 2 |
S9(3) COMP-3 | 2 |
S9(7) COMP-3 | 4 |
S9(8) COMP-3 | 5 |
S9(13) COMP-3 | 7 |
S9(16) COMP-3 | 9 |
S9(18) COMP-3 | 10 |
If you want to see COMP-3 values in sequential file since it is stored in decimal form, You need to use HEX ON commmand to see COMP-3 values,
When you use this command it will show two lines per each record. You need to read the comp-3 value from left, top to bottom (in two
lines) and then move to second character read top to bottom.
For Example :
+87634 will be displayed as 86473C
-4567 will be displayed as 05746D
last characters represents sign.
If last character is C or F, that denotes a positive sign.
If last character is D , that denotes a negative sign.
Displaying COMP-3 characters:
Before displaying COMP-3, Move the data
to usage display variable then display. If it holds Sign then Use SIGN
LEADING SEPARATE which uses one extra byte for sign and sign will be
displayed separately.
01 WS-NUMBER PIC S9(03) COMP-3.
01 WS-NUMBER1 PIC S9(03) SIGN LEADING SEPARATE.
.........
MOVE WS-NUMBWER TO WS-NUMBER1.
DISPLAY WS-NUMBER1.
|
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!