Open In App

Difference between COMP and COMP3

Last Updated : 03 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Internally the computer stores the data in more than one form. The Cobol language facilitates the programmer to specify the  internal representation of the data according to the need. There are two internal forms available in the Cobol:

  1. DISPLAY –
    It is the default internal representation of the data. Any type of data can be specified with the DISPLAY internal representation.
  2. COMPUTATIONAL –
    Only the numeric data’s can be specified with COMPUTATIONAL internal representation. There are many types of COMPUTATIONAL representation, like COMP, COMP-1 , COMP-2, COMP-3 etc.

USAGE clause is used to specify the type of internal representation. You can use any level-number for USAGE clause except 66 or 88.

Syntax:
        USAGE IS {COMPUTATIONAL/COMP/DISPLAY}.

1. COMP :
Usage clause is applicable only on numerical data items. It represents the data purely in binary form. and can store the data either in half word or in full word depending on the size of the data. We can use only 9 and S during the data declaration:

  • 9 is used to store declare integer variables.
  • S is used to store the sign.

2. COMP3 :
Usage clause is applicable only on numerical data items. It stores the data in packed decimal form. It uses one rightmost bit to store the sign irrespective of whether we have used S in PIC clause or not. The hexadecimal number C and F stores the positive sign at rightmost bit and D stores the negative sign at rightmost bit. We can use 9, S and V in PIC clause during data declaration.

V is used to store decimal point at a particular location in data item.

Difference between COMP and COMP3 :

COMP

COMP3

It represents the data in pure binary form. It represents the data in packed decimal form.
We can use only 9 and S in PIC Clause. We can use 9 , S , V in PIC Clause.
COMP usage stores the data in half word or in full word, depending on the size of the data. COMP3 usage stores 1 digit in half byte (i.e. 4 bits) and a separate 1 bit is reserved for the sign, which is stored at the right side of the data.

The memory to be occupied by the data according to the length is predefined i.e. :

  • 9(01) – 9(04) : 16 bits (2 bytes)
  • 9(05) – 9(09) :  32 bits (4 bytes)
  • S9(10) – S9(18) :  64 bits (8 bytes)

The memory to be occupied by the data is defined by the following formula:

  • (length of variable + 1)/2 bytes.

Example : The memory occupied by S9(3) is:

(3+1)/2 i.e. 2 bytes.

COMP does not occupy extra space to store sign.  In COMP3 sign in compulsorily stored at right side and thus it occupies an extra space.
Example:
        02 CompVariable PIC 9 USAGE IS COMP.
        02 CompVariable1 PIC S9(5) USAGE IS COMP.
Example:
        02 Variable PIC 9 USAGE IS COMP3.
        02 Variable1 PIC S9(10) USAGE IS COMP3.
        02 Variable2 PIC S9V99 USAGE IS COMP3.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads