In this topic, we described about the below sections -
A literal is a character-string value formed by either set of characters or a figurative constant. Literals are six types –
- Boolean literals
- DBCS literals [Not Covered]
- National literals [Not Covered]
- Non-numeric literals
- Null-terminated non-numeric literals [Not Covered]
- Numeric literals
Boolean literals -
Boolean literal is a set of characters delimited by the separator B" at the left side and right side by single quotation mark ("). Boolean literal can contain only one character 0 or 1 that excludes the delimiter separators.
Nonnumeric literals -
Non-numeric literals are character-stings enclosed by quotation marks(" ") or apostrophes(''). Non-numeric literals can contain any allowed character from the EBCDIC character set (A-Z, a-z, 0-9 and special characters).
For example - "HELLO", "123", "H1234", 'HI', '123', 'ABC123' etc,.
Non-numeric literals follow below guidelines -
- The enclosing quotation marks("") or apostrophes('') are excluded from the literal when the program compiled.
For example - The literal value from "HELLO" is HELLO and similarly, 123 from '123'.
- The Nonnumeric literal must be represented by a pair of quotation marks ("") or a pair of apostrophes ('') respectively. i.e., the opening delimiter should match with the ending delimiter.
- The opening and closing delimiter character must be the same for a literal.
For example - if the literal represention starts with quotation mark("), ending should code with quotation mark("). Similarly, if the literal representation starts apostrophes('), ending should code with apostrophes(').
- Any punctuation characters can be included as part of Non-numeric literal.
For example - "123,23,45", "HI; HOW ARE YOU?".
- Non-numeric literal minimum length is one character.
For example - "1", "A" etc,.
- The non-numeric literal maximum length is 256 characters.
For example - "1234567890....", "MAINFRAME SYSTEMS ARE LENGENDARY SYSTEMS............." etc,.
- Non-numeric literals are in the Non-numeric data type and category. i.e., non-numeric literals are of type alphabetic or alpha-numeric.
For example - 01 WS-NLIT PIC X(20) VALUE "ABC1234563SD". or 01 WS-NLIT PIC A(20) VALUE "ABC".
|Correct Representation||Wrong Representation|
'THIS IS CORRECT'
'THIS IS WRONG"
"THIS IS CORRECT"
"THIS IS WRONG'
"THIS ISN""T WRONG"
"THIS ISN'"T CORRECT"
'THIS ISN''T WRONG'
'THIS ISN'"T CORRECT'
For example - 'THIS ISN''T WRONG' is converted to THIS ISN'T WRONG.
Numeric literals -
A numeric literal is a character-string, and the character-string can be a combination of digits 0 through 9, a sign character (+ or -), and a decimal point.
The size of a numeric literal is equal to the number of digits specified by the user. If the literal contains no decimal point, then it is an integer.
Numbers algebraically specify every literal numeric value, and every numeric literal is of numeric data type.
For example - 123, -256, +3030, 123.34, +123.34, -345.67 etc,.
Numeric literals follow the below rules -
- The minimum length is 1, and the maximum length is 18 digits if the (default) compiler option *NOEXTEND or the PROCESS statement option NOEXTEND is in effect.
For example - -1, -123456789012345678, +1, +123456789012345678 etc,.
The minimum length is 1, and the maximum length is 31 digits, if the arithmetic mode compiler option *EXTEND31 or PROCESS statement option EXTEND31 is in effect.
For example - -1, -123456789012345678.... (31 digits), +1, +123456789012345678..... (31 digits) etc,.
The minimum length is 1, and the maximum length is 34 digits, if the arithmetic mode compiler option *EXTEND31FULL or PROCESS statement option EXTEND31FULL is in effect.
For example - -1, -123456789012345678.... (34 digits), +1, +123456789012345678.... (34 digits) etc,.
The minimum length is 1, and the maximum length is 63 digits, if the arithmetic mode compiler option *EXTEND63 or PROCESS statement option EXTEND63 is in effect.
For example - -1, -123456789012345678..... (63 digits), +1, +123456789012345678..... (63 digits) etc,.
- Allows only one sign character. If the sign is specified as part of literal, it should be the literal's first character.
For example - -123, +123, 123 are valid. 1-23, 12-3, 123- are not valid.
- If the literal is unsigned or no sign is specified, it is a positive value.
For example - If 123 is specified, then it is +123.
- Allows only one decimal point. If a decimal point is specified in the literal, it is considered as an assumed decimal point (i.e, it is not counted as a character position in the literal). The decimal point can be specified anywhere within the literal except as the last character of the literal.
For example - -123.34, -12.345, -1.23, -.234 are valid. -234., +234. are not valid.
Numeric literals are again classified as two types and those are -
- Fixed-point numbers
- Floating-point numbers
Fixed-point numeric literals -
Fixed-point numeric literals are specified as a number value with a sign value directly (i.e., without mantissa and exponent).
For example - +124567, 1234, -99382 etc,.
Floating-point numeric literals -
Floating-point numeric literals are specified in the form of mantissa and exponent. Below format specifies floating-point literal values -
For example - +9.999E-3 (equal to 0.009999), where 9.999 is mantissa and 3 is exponent.
Floating-point numeric literals follow the below rules -
- The sign value is optional before the mantissa and the exponent. If the sign is ignored or not specified, the compiler considers the literal as positive.
For example - 9.999E-3 is same as +9.999E-3.
- The mantissa value falls between 1 and 16 digits. A decimal point should be part of the mantissa.
For example - 9.999E-3. 9.999 is in between 1 and 16.
- The exponent value should be specified followed by an E, in one/two/three digits, and the sign is optional for the exponent.
For example - 9.999E-3. i.e., 0 < 3 < 999.
- The floating-point literal magnitude value falls between the range 0.54E-78 and 0.72E+76.
Note! An E-level diagnostic message is generated for values outside of this range, and the value is replaced by either 0 or 0.72E+76.
Practical Example -
Scenario - Below example describes how the literals declared in COBOL program.
----+----1----+----2----+----3----+----4----+----5----+----6----+----7-- ***************************** Top of Data ****************************** IDENTIFICATION DIVISION. PROGRAM-ID. LITERAL. AUTHOR. MTH. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-VARIRABLES. 05 WS-NNUM-LIT PIC X(40) VALUE "COBOL IS LEGACY PROGRAMMING LANGUAGE". 05 WS-NUM-LIT PIC 9(05) VALUE 256. 05 WS-FP-NUM-LIT PIC S9(05) VALUE -128. 05 WS-FLP-NUM-LIT PIC -99V9(3)E-99. PROCEDURE DIVISION. DISPLAY "NON-NUMERIC LITERAL: " WS-NNUM-LIT. DISPLAY "NUMERIC LITERAL: " WS-NUM-LIT. DISPLAY "FIXED-POINT NUMERIC LITERAL: " WS-FP-NUM-LIT. MOVE -9.999E-3 TO WS-FLP-NUM-LIT. DISPLAY "FLOATING-POINT NUMERIC LITERAL: " WS-FLP-NUM-LIT. STOP RUN. **************************** Bottom of Data ****************************
Explaining Example -
In the above example, we have shown different literal declarations and how they are initialized. WS-NNUM-LIT is the non-numeric literal variable assigned with non-numeric literal COBOL IS LEGACY PROGRAMMING LANGUAGE.
WS-NUM-LIT, WS-FP-NUM-LIT are fixed-point numeric literal variables assigned with 256 and -128, respectively. WS-FLP-NUM-LIT is a floating-point numeric literal variable and assigned with -9.999E-3.
The input value mantissa is 9.999(without decimal). However, the declaration is 99V9(3). So the mantissa changed to 99.990(without decimal), and the exponent got decreased by 1. i.e., the exponent is changed from -3 to -4. The final value displayed is -99990E-4.