Constants in COBOL On this page Constants in Cobol are defined using the 77
or 88
levels for literals. In this example, we will use 77
to define constant values. Here’s the explanation and code translated for use in Cobol:
Cobol supports constants of character, string, boolean, and numeric values.
Constant Declaration# 77
declares a constant value.
IDENTIFICATION DIVISION .
PROGRAM-ID . ConstantsExample .
DATA DIVISION .
WORKING-STORAGE SECTION .
77 s PIC X(8) VALUE "constant" .
Main Procedure# In the main procedure, we will print the constant value and demonstrate its usage.
PROCEDURE DIVISION .
DISPLAY s .
* A ` 77 ` level statement can appear anywhere a ` 01 ` level
* statement can .
77 n PIC 9(9) VALUE 500000000 .
* Constant expressions perform arithmetic with
* arbitrary precision .
77 d USAGE COMP-2 VALUE 3 E + 20 / 500000000 .
DISPLAY d .
* To handle large numbers , Cobol has intrinsic functions
* such as INTEGER-OF-DATE and SIN , but without
* external libraries , we will show simple operations .
77 large-number PIC 9(16) .
MOVE FUNCTION INTEGER-OF-DATE ( 20230101 ) to large-number .
DISPLAY large-number .
* While displaying sine won 't be demonstrated due to lack of intrinsic math library
* functions , the concept requires conversion .
STOP RUN .
Execution Output# To run the program, create a file named ConstantsExample.cbl
and use a Cobol compiler.
$ cobc -x ConstantsExample.cbl
$ ./ConstantsExample
constant
6E+11
20230101
Now that we can run and build basic Cobol programs, let’s learn more about the language.