Microsoft KB Archive/66478

COBOL Uninitialized Numeric Variables Give Bad Results

PSS ID Number: Q66478 Article last modified on 04-20-1993

4.00 | 4.00 MS-DOS | OS/2

Summary: By default, COBOL will initialize all data items to spaces if no VALUE clause has been specified in the WORKING-STORAGE SECTION. This includes numeric items. The results of doing arithmetic on such an item can be unpredictable. To avoid problems, all numeric items should be initialized to numeric values before use. This is discussed on page 8-6 in the “Microsoft COBOL 4.0: Operating Guide.”

More Information: The program below generates an intermediate code error “Illegal character in numeric field (163)” when animated. To produce the error, compile with the ANIM directive, then animate.

Code Example
$SET ANS85 IDENTIFICATION DIVISION. PROGRAM-ID. INITPROG. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 1ST-FIELD     PIC 9(5) VALUE 8. 01 2ND-FIELD     PIC 9(5). 01 ACCUM         PIC 9(10). 01 JUNK          PIC X.   PROCEDURE DIVISION. 100-MAIN. DISPLAY &quot;Lets start...&quot; WITH BLANK SCREEN. * At this point we have initialized 1st-field but not * 2nd-field. DISPLAY &quot;1ST:&quot; AT 1105. DISPLAY 1ST-FIELD AT 1110. DISPLAY &quot;2ND:&quot; AT 1205. DISPLAY 2ND-FIELD AT 1210. ACCEPT JUNK AT 1310. * Arithmetic operation using an uninitialized variable results * in &quot;Illegal character in numeric field (error 163)&quot;. ADD 1ST-FIELD TO 2ND-FIELD GIVING ACCUM. STOP RUN. Additional reference words: 4.00 Copyright Microsoft Corporation 1993.