Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Getting Started

Source Format and Coding Rules

Learn COBOL fixed and free source format: columns 1-72, area A and B, comments, continuation lines, names, literals, figurative constants and reserved words.

COBOL was designed for 80-column punched cards, and its traditional fixed format still follows the card layout. Where you put text on a line matters. Newer free format removes the column rules. You must know the fixed rules because almost all existing COBOL uses them, and this lesson also covers the building blocks of every statement: names, literals and figurative constants.

Fixed format columns

Columns Name Use
1-6 Sequence number area Ignored by the compiler. Often blank or line numbers.
7 Indicator area * or / = comment, - = continuation, D = debugging line, space = normal.
8-11 Area A Division, section and paragraph names, FD, level numbers 01 and 77 start here.
12-72 Area B Statements and other entries.
73-80 Identification area Ignored. Anything here is not compiled.

The "text starts in column 8 or later" rule is why the examples have at least seven leading spaces. A line that runs past column 72 is silently cut, which causes baffling errors.

COBOL
      * This is a comment line (asterisk in column 7)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COLUMNS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 GREETING PIC X(20) VALUE "Hello from area A".
       PROCEDURE DIVISION.
       MAIN-PARAGRAPH.
           DISPLAY GREETING
           STOP RUN.

MAIN-PARAGRAPH. starts in area A (column 8). The DISPLAY and STOP statements start in area B (column 12). Most compilers, GnuCOBOL included, are tolerant about area A and B, but mainframe compilers can reject misplaced items, so build the habit now.

Python Column checker (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Continuation lines

A literal or word too long for one line continues on the next line with a hyphen in column 7. For a literal, the first line runs to column 72 without a closing quote, and the continuation line starts with a quote:

COBOL
       01 LONG-TEXT PIC X(60) VALUE "This message is too long to fit on one
      -    "line of source".

Beginners often avoid the problem by splitting long text into several DISPLAY operands or several VALUE items instead.

Free format

With >>SOURCE FORMAT IS FREE at the top of the file (or cobc -free), there are no column rules, *> begins a comment anywhere on a line, and long literals can be joined with &.

COBOL
>>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. FREEDEMO.
PROCEDURE DIVISION.
    *> This comment can sit anywhere
    DISPLAY "Free format " &
            "is easier to type"
    STOP RUN.

Names

User-defined names (data items, paragraphs, programs) follow these rules:

  • 1 to 30 characters in COBOL-85 (GnuCOBOL allows more, but portable code stays at 30; COBOL 2002 allows up to 31).
  • Letters, digits and hyphens only. Names are not case sensitive (Total and TOTAL are the same).
  • Must not begin or end with a hyphen.
  • A data name needs at least one letter. Paragraph names may be all digits, though that is unwise.
  • It must not be a reserved word. DATE, TIME, NAME, COUNT, NUMBER and END are the usual accidental collisions in real code. Prefix your names (WS-DATE, CUST-NAME) to avoid them.

COBOL programmers use hyphenated, meaningful names: CUSTOMER-BALANCE, not CB. Group prefixes (WS- for working storage, LS- for linkage, IN-, OUT-) help you read a large program.

Literals

  • Numeric literals: 42, -7, 3.14, +0.5. They have no quotes and may have a sign and a decimal point, but no commas or exponent.
  • Nonnumeric (alphanumeric) literals: "Hello" or 'Hello'. To include the quote character, double it: "say ""hi""".
  • Hexadecimal literal: X"41" is the single byte A.
  • Boolean/National literals: B"1010", N"..." exist but are rarely needed at first.

Figurative constants

These are named values that fill a field regardless of its size:

Constant Value
ZERO, ZEROS, ZEROES Numeric 0 or the character 0 repeated
SPACE, SPACES The space character repeated
HIGH-VALUE, HIGH-VALUES The highest byte (hex FF)
LOW-VALUE, LOW-VALUES The lowest byte (hex 00)
QUOTE, QUOTES The quote character "
ALL "AB" The literal AB repeated to fill the receiving field
COBOL
       01 LINE-OF-STARS PIC X(10).
       ...
           MOVE ALL "*" TO LINE-OF-STARS
Python Figurative constants fill any size (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Punctuation and sentences

  • A sentence is one or more statements ending in a period.
  • A period followed by a space (or the end of the line) is a terminator; a period inside a number or literal is not.
  • Commas and semicolons are optional separators and mean the same as a space. Always put a space after them.
  • Operators + - * / ** and comparison symbols need spaces around them: A + B, not A+B.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In fixed format, which column holds the comment indicator *?

    Column 7 is the indicator area. An asterisk there makes the whole line a comment.
  2. 2. What happens to text after column 72 in fixed format?

    Columns 73-80 are the identification area and are not compiled, so overlong lines are silently truncated.
  3. 3. Which is a valid COBOL user-defined data name?

    Names may not start or end with a hyphen, and a data name needs at least one letter.
  4. 4. What does MOVE ALL "AB" TO a PIC X(5) field store?

    ALL repeats the literal from its start until the receiving field is full: ABABA.

Summary

  • Fixed format: columns 1-6 sequence, 7 indicator, 8-11 area A, 12-72 area B, 73-80 ignored.
  • Free format (-free or >>SOURCE FORMAT IS FREE) drops the column rules and uses *> comments.
  • Names use letters, digits and hyphens, up to 30 characters, and must avoid reserved words.
  • Literals are numeric, quoted alphanumeric or hexadecimal; figurative constants such as SPACES and ZEROS fill any field.
  • Sentences end with a period; put spaces around operators.

Next: Program Structure Overview