Problem:
Mapping numeric data types from COBOL to C may cause some unpredictable results when the data items are passed to C and assigned values in C, especially when these are COMP-5 numeric data types.
Resolution:
C always uses a whole word of memory (4-bytes) when a numeric variable is passed to it as an int or a long int.
Data items defined as type COMP-5 in COBOL will store:
1-4 decimal digits in 2 bytes of storage;
5-9 decimal digits in 4 bytes of storage; and
10-18 decimal digits in 8 bytes of storage.
This must be considered when passing numeric variables between COBOL and C.
For example, consider the following COBOL data structure:
01 ws-group.
03 ws-a PIC 9(4) COMP-5.
03 ws-b PIC 9(9) COMP-5.
When ws-group is passed as a parameter to a C program, C finds ws-a in 2 bytes of memory, with part of ws-b making up the remaining 2 bytes of memory for the first int argument. This will cause unpredictable results when C assigns values to these memory addresses and the data items are DISPLAYed from back within the COBOL code.
The resolution to this is to make sure all numeric values passed to C begin at the start of a memory address for a word (4 bytes). For the example used, a filler field of 2 bytes (PIC XX), will ensure that ws-b starts at the next word in memory.
Revised data structure:
01 ws-group.
03 ws-a PIC 9(4) COMP-5.
03 filler PIC XX.
03 ws-b PIC 9(4) COMP-5.