• A literal is a group of characters or a group of words, or a figurative constant.
  • Literals are always specifying with a VALUE clause.
  • Literals are classified into two types –
    • Non-numeric literals
    • Numeric literals

Nonnumeric literals -

Non-numeric literals are character-stings or set of characters enclosed by quotation marks(" ") or apostrophes(' ').

Non-numeric literals can contain any allowed character from the character set (A-Z, a-z, 0-9 and special characters).

Syntax -

 level-number variable-name 
				PIC data-type-character(variable-length) 
				VALUE "single-byte-characters"

 level-number variable-name 
				PIC data-type-character(variable-length) 
				VALUE 'single-byte-characters'
 
 level-number variable-name 
				PIC data-type-character(variable-length) 
				VALUE non-numeric-figurative-constant

For example - Declaring a variable of alphabetic type to store a value HELLO.

 01 WS-VAR      PIC A(05) VALUE "HELLO".
  • level-number - Specifies the level number of the declaration from 01 to 49. In the above example, it is 01.
  • variable-name - Specifies the name of the variable. In the above example, it is WS-VAR.
  • data-type-character - Specifies the type of the variable. In the above example, it is A.
  • variable-length - Specifies the variable length to store the data. In the above example, it is 05.
  • single-byte-characters - Specifies the literal character string. In the above example, it is HELLO.

Guidelines -

Non-numeric literals follow the 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, HELLO from 'HELLO'.
  • 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"
    ...
  • The non-numeric literal maximum length is 256 characters.
    For example -
    "1234567890...."
    "MAINFRAME SYSTEMS ARE LENGENDARY SYSTEMS............."
    ...
  • 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".
    01 WS-NLIT PIC A(20) VALUE "ABC".

Representation -

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'
Note! A double apostrophe ('') is converted to a single apostrophe when the double apostrophe is a delimiter.
For example - 'THIS ISN''T WRONG' is converted to THIS ISN'T WRONG.

Numeric literals -

  • A numeric literal is a numeric value that is a combination of digits 0 through 9, a sign character (+ or -), and a decimal point.
  • A numeric literal specifies directly without quotation marks(" ") or apostrophes(' ').
  • The size of a numeric literal is equal to the number of digits specified by the user.
  • Every numeric literal is of numeric data type.

Syntax -

 level-number variable 
				PIC data-type-character(variable-length) 
				VALUE numeric-value.
				
 level-number variable 
				PIC data-type-character(variable-length) 
				VALUE numeric-figurative-constant

For example - Declaring a variable of alphabetic type to store a value HELLO.

 01 WS-VAR      PIC 9(04) VALUE 1234.
  • level-number - Specifies the level number of the declaration from 01 to 49. In the above example, it is 01.
  • variable-name - Specifies the name of the variable. In the above example, it is WS-VAR.
  • data-type-character - Specifies the type of the variable. In the above example, it is 9.
  • variable-length - Specifies the variable length to store the data. In the above example, it is 04.
  • numeric-value - Specifies the numeric value. In the above example, it is 1234.

Rules -

Numeric literals follow the below rules -

  • The minimum length is 1, and the maximum length is 18 digits.
    For example - -1, -123456789012345678, +1, +123456789012345678 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.
Warning! If the numeric literal is enclosed with quotation marks, the system considers it as non-numeric literal.

Types -

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 numbers with a signed value (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 -

Floating-point Numeric Literals
 [+/-] mantissa E [+/-] exponent

For example - +9.999E-3 (equal to 0.009999), where 9.999 is mantissa and 3 is 
exponent.

Rules -

  • The sign value is optional before the mantissa and the exponent. If the sign is not specified, the compiler considers it 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.

Code -

Literal program Code
----+----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 ****************************

Output -

Literal program Output

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.