1. Home
  2. Knowledge Base
  3. Developers
  4. Application Development
  5. COBOL Development
  6. Hint: PIC 9(4) COMP COBOL can hold integer values from 0 to 65,536

Hint: PIC 9(4) COMP COBOL can hold integer values from 0 to 65,536

The answer might not be what you expect.

You might think a PIC 9(4) COMP Variable in COBOL can hold up to 4 digit numbers or from 0 to 9,999. But the PIC 9(4) COMP variable can hold up to the value 65,535, or approximately 2^16.

If the variable holds the value 65,535 and your program adds 1, the result will be 0.

When using a signed variable, PIC S9(4) COMP, the range is from -32,768 to 32,767, or approximately 2^15, with one bit used to store the negative indicator.

Use Case Example

A problem with the Service Purchase Id (SP_ID) was discovered to be related to the range of a PIC 9(4) COMP variable. The variable W-SPID is defined as PIC 9(4) COMP in the PAPPCDED module, but all other variables that hold the Service Purchase Id are defined as PIC 9(11) COMP-3.

As the service purchase IDs incremented beyond 65535, the new purchases were not processed correctly. The SP ID written to the service purchase payments table was 65536 lower than the correct SP ID – and downstream pages and processes couldn’t correctly link the payment details to the other service purchase details.

Ensure the variables you use or create are large enough to hold the expected values. COBOL does not return an error when assigning a value larger than the defined variable size, so identifying problems and the appropriate solution can be complex.

Also, when dramatically increasing an array’s size, ensure that the indexes, counters, and maximum variables are large enough to accommodate the increased array size. (more…)

Was this article helpful?

Related Articles

Leave a Reply